$ cat color4
echo There are $#
comand line argument
echo They are $*
ehco The first command line argument is $1
$ chmod +x color4
$ color4 red green yellow blue
They are 4 command line arguments
They are red green yellow blue
The first command line argument is red
$
$ cat > my_install2
echo $0 will install $#
files to your bin directory
echo The files to be installed are : $*
chmod +x $*
mv $* $HOME/bin
echo Installaton is complete
ctril + d
$ chmod +x my_install2
$ my_install2 color1 color2
my_intall2 will install 2
files to your bin directory
The files to be installed are: color1,color2
Intallaiton is complete
$ cat list_dir
cd $*
echo You are in the $(pwd) directory
echo The contents of the directory are:
ls –F
$ list_dir dir1 dir2 dir3
h: cd: bad argument count
由于cd命令不能同时进入到多个目录中,这个程序会发生错误。
1.6 shift 命令
向左移动所有的在*中的字符串n个位置
#的数目减少n个(n的默认值是1)
语法:shift [n]
例子:
$ cat color5
orig_args=$*
echo There are $#
command line arguments
echo They are $*
echo Shifting two arguments
hift 2
echo There are $#
comand line arguments
echo They are $*
echo Shifting two arguments
hift 2; final_args=$*
echo Original arguments are: $orig_args
echo Final arguments are: $final_args
$ color5 red green yellow orange black
There are 6 command line arguments
They are red green yellow blue orange black
Shifting two arguments
There are 4 command line arguments
They are yellow blue orange black
Shiftging two arguments
Original arguments are:
red green yellow blue orange black
Final argument are : orange black
$
1.7 read 命令
语法:
read variable [variable......]
例子:
$ cat color6
echo This program prompts for user input
echo “please enter your
favorite two colors -> \c”
read color_a color_b
echo The colors you entered are:
$color_b $color_a
$ chmod +x color6
$ color6
This program prompts for user input
Please enter your favorite two colors -> red blue
The colors you entered are: blue red
$ color6
This program prompts for user input
Please enter you favorite two colors -> red blue tan
The color you enterd are :blue tan red
$ cat > my_install3
echo $0 will install files into your bin directory
echo “Enter the names of the files -> \c”
read filenames
mv $filenames $HOME/bin
echo Instllation is complete
ctrl + d
$ chmod +x my_install13
$ my_install13
my_install13 will install files into your bin directory
Enter the names of the files -> f1 f2
Installaton is complete