expression1 -eq expression2
expression1 -ne expression2
expression1 -gt expression2 expression1 expression2
expression1 -ge expression2 expression1 expression2
expression1 -lt expression2 expression1 expression2
expression1 -le expression2 expression1 expression2
! expression
多重条件判定、例えばtest-r filename-a-x filename-a
-o
!
3、制御構造 (1)if
:
if condatation if condtaion then
then statements
statements ====== elif
elif statements
statements else
else statements
statements fi
fi
: if [ -f file ]; then
(2)for
:
for variable in values
do
statements
done
:
: 1 100 for variable in $ ( seq 1 100 );
(3)while
:
while condation do
statements
done
(4)until
:
until condaion
do
statements
done
(5)case
:
case variable in
pattern [ | pattern ] …) statements;;
pattern [ | pattern ] …) statements;;
…..
esac
(6)
AND :statement1 && statements2 && statements3 && ….
OR :statements1 || statements2 || statements3 || …
(7)
, 。
4、
:
function fname() {
.....
}
、
1、break: , for、while until 。
2、”:” : 。 , true 。
while true ===== while :
3、continue: for、while until 。
4、”.” : shell
, , shell, , , , shell。 source , shell。
5、echo : 。
:
echo -n “string to output”
echo -e “string to output \c”
6、eval : 。
7、exit n : n 。
126
127
128
8、export : shell , shell 。 , shell shell ( )shell 。Export , 。 , shell 。
9、expr == $((……)) , $( ) 。
10、trap : 。
。
trap command signal
trap "rm -f temp_file" EXIT
11、find : 。
: find [path] [options] [tests] [actions]
-depth
-follow
-maxdepths N N
-mount
:
12、grep( General Regular ExpresiionPraser):
: grep [options] PATTERN [FILES]
options:
-c ,
-E
-h ,
-i
-l ,
-v
13、
正規表現、通常grepと組み合わせて使用
^
$
.
[] ,
特殊マッチングモード[:alnum:]
[:alpha:]
[:ascii:]
ASCII
[:blank:]
[:cntrl:]
ASCII
[:digit:]
[:graph:]
、
[:lower:]
[:print:]
[:punct:]
[:space:]
[:upper:]
[:xdigit:]
、Shell script
#!/bin/bash
#This is a program about CD collection
#Copyright (C) 2014
#command line description
#wc
#wc(Word Count) 、 、
#
#-c 。
#-l
#-m 。 -c
#-w
#grep
#
#
#grep [options] PATTERN [FILES]
#options
#-c
#-E
#-h
#-i
#-l
#-v
#cut
#cut , , 。 , “ ” , 。
#cut [-bn] [file] cut [-c] [file] cut [-df] [file]
#cut 、 、 。
# File ,cut 。 -b、-c -f 。
#
#-b: 。 , -n 。
#-c: 。
#-d: , 。
#-f : -d , 。
#-n: 。 -b 。 -b List
, ; , 。
#set envrironment variable
menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file="cdb.$$"
trap 'rm -f $temp_file' EXIT
get_return(){
echo -e "Press return \c"
read x
return 0
}
get_comfire(){
echo -e "Are you sure? \c"
while true
do
read x
case "$x" in
y | yes | YES | Yes )
return 0;;
n | NO | No | N )
echo "Canclled!"
return 1;;
* ) echo "Pleasse enter yes or no!"
esac
done
}
# Menu
set_menu_choice(){
clear
echo "Options :-"
echo
echo " a) Add new CD"
echo " f) Find CD"
echo " c) Count the CDs andtracks in the catalog"
if [ "$cdcatnum" != "" ]; then
echo " l) List tracks on$cdtitle"
echo " r) Remove$cdtitle"
echo " u) Update trackinformation for $cdtitle"
fi
echo " q) Quit"
echo
echo -e "Please enter choice then press retrun \c"
read menu_choice
return
}
insert_title(){
#insert information to title_file
echo $* >> $title_file
return
}
insert_track(){
echo $* >> $tracks_file
return
}
add_record_tracks(){
echo "Enter track information for this CD"
echo "When no more tracks enter q"
cdtrack=1
#define the title of track
cdttitle=""
#set $cdttitle = null
while [ "$cdttitle" != q ]
do
#input title
echo -e "Track $cdtrack, track title? \c"
read tmp
cdttitle=${tmp%%,*}
if [ "$tmp" != "$cdttitle" ]; then
echo "Sorry, no commas allowed"
continue
fi
if [ -n "$cdttitle" ]; then
if [ "$cdttitle" != "q" ]; then
insert_track $cdcatnum,$cdtrack,$cdttitle
fi
else
cdtrack=$((cdtrack-1))
fi
cdtrack=$((cdtrack+1))
done
}
add_records(){
echo -e "Enter catalog name \c"
read tmp
cdcatnum=${tmp%%,*}
echo -e "Enter title \c"
read tmp
cdtitle=${tmp%%,*}
echo -e "Enter type \c"
read tmp
cdtrack=${tmp%%,*}
echo -e "Enter artist/composer \c"
read tmp
cdac=${tmp%%,*}
#Check that they want to enter the information
echo About to add new entry
echo "$cdcatnum $cdtitle $cdtype $cdac"
#if comfired then append it to the tille_file
if get_comfire ; then
insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
add_record_tracks
else
remove_records
fi
return
}
find_cd(){
if [ "$1" = "n" ]; then
asklis=n
else
asklist=y
fi
cdcatnum=""
echo -e "Enter a string to search for in the CD titles \c"
read searchstr
if [ "$searchstr" = "" ]; then
return 0
fi
if [ ! -e $temp_file ]; then
echo"temp_file don'texist!!!!!!!!"
fi
grep "$searchstr" $title_file > $temp_file
set $(wc -l $temp_file)
linesfound=$1
case "$linesfound" in
0 ) echo "Sorry, nothing found"
get_return
return 0
;;
1 ) ;;
2 ) echo "Sorry, not unique."
echo "Found the following"
cat $temp_file
get_return 0
;;
esac
IFS=","
read cdcatnum cdtitle cdtype cdac < $temp_file
IFS=" "
if [ -z "$cdcatnum" ]; then
echo "Sorry, could not extract catalog filed from $temp_file"
get_return
return 0
fi
echo
echo Catalog number: $cdcatnum
echo Title: $cdtitle
echo Type: $cdtype
echo Artist: $cdac
echo
get_return
if [ "$asklist" == "y" ]; then
echo -e "View tracks for this CD? \c"
read x
if [ "$x" == "y" ]; then
echo
list_tracks
echo
fi
fi
return 1
}
update_cd(){
if [ -z "$cdcatnum" ]; then
echo "You must select a CD"
find_cd n
fi
if [ -n "$cdcatnum" ]; then
echo "Current tracks are:-"
list_tracks
echo
echo "this will re-enter the tracks for $cdtitle"
get_comfire && {
grep -v "^${cdcatnum}," $tracks_file > $temp_file
mv $temp_file $tracks_file
echo
add_record_tracks
}
fi
return
}
count_cds(){
#wc -l file : statistic thecounts
set $(wc -l $title_file)
num_titles=$1
set $(wc -l $tracks_file)
num_tracks=$1
echo found $num_titles CDs with a total of $num_tracks tracks
get_return
return
}
remove_records(){
if [ -z "$cdcatnum" ]; then
echo "You must select a CD"
find_cd n
fi
if [ -n "$cdcatnum" ]; then
echo "You are about to delete $cdtitle"
get_comfire && {
grep -v "^${cdcatnum}," $title_file > $temp_file
mv $temp_file $title_file
grep -v "^${cdcatnum}," $tracks_file > $temp_file
mv $temp_file $tracks_file
cdcatnum=""
echo Entry removed
}
get_return
fi
return
}
list_tracks(){
if [ "$cdcatnum" == "" ]; then
echo "No CD selected yet"
return
else
grep -v "^${cdcatnum}," $tracks_file > $temp_file
num_tracks=$(wc -l $temp_file)
if [ "$num_tracks" = "0" ]; then
echo no tracks found for $cdtitle
else {
echo
echo "$cdtitle :-"
echo
cut -f 2- -d , $temp_file
echo
} | ${PAGER:-more}
fi
fi
get_return
return
}
#rm -f $temp_file
if [ ! -f $title_file ]; then
touch $title_file
fi
if [ ! -f $tracks_file ]; then
touch $tracks_file
fi
if [ ! -f "$temp_file" ]; then
touch $temp_file
fi
#Now the application proper
clear
echo
echo
echo "Mini CD manager"
sleep 1
quit=n
if [ -e $temp_file ]; then
echo "temp_file don't exist!!!"
return EXIT
fi
while [ "$quit" != "y"]; do
set_menu_choice
case "$menu_choice" in
a) add_records;;
r) remove_records;;
f) find_cd y;;
c) count_cds;;
l) list_tracks;;
b)
echo
more $title_file
echo
get_return;;
q| Q ) quit=y;;
*) echo "Sorry, choice not recoginzed";;
esac
done
#Tidy up and leave
rm -f $temp_file
echo "Finished"
exit 0
CD
:
CD:
CD:
: