Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

UNIX_slaydy_2015_lek4

.pdf
Скачиваний:
9
Добавлен:
05.06.2015
Размер:
464.35 Кб
Скачать

 

Специальные переменные shell: # и *

#

Количество аргументов в командной строке

*

Строка, содержащая все аргументы командной строки (за

 

исключением $0)

Пример:

$ cat color4

echo There are $# command line arguments echo They are $*

echo The first command line argument is $1

$ chmod +x color4

$ ./color4 red green yellow blue The are 4 command line arguments They are red green yellow blue

The first command line argument is red

$

21

Специальные переменные shell: # и *

Пример:

$ 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 Installation is complete

$ chmod +x my_install2

$ my_install2 color1 color2

my_install2 will install 2 files to your bin directory The files to be installed are: color1 color2 Installation is complete

22

Специальные переменные shell: # и *

Пример:

$ cat list_dir cd $*

echo You are in the $(pwd) directory echo The contents of this directory are: ls -F

$ list_dir dir1 dir2

sh: cd: bad arguments count

23

Команда shift

•Сдвигает все строку аргументов влево на n позиций

•Уменьшает переменную # на n (по умолчанию n=1)

Синтаксис:

shift [n]

Команду shift полезно использовать для:

•доступа к позиционным параметрам в группах, например, совокупности координат x и y

•удаления опций из командной строки

24

Команда shift

Пример:

$ cat color5 orig_args=“$*”

echo There are $# command line arguments echo They are $*

echo Shifting two arguments shift 2

echo There are $# command line arguments echo They are $*

echo Shifting two arguments shift 2; final_args=$*

echo Original arguments are: $orig_args echo Final arguments are: $final_args

25

Команда shift

$ color5 red

green

yellow blue 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

 

Shifting two arguments

 

 

 

 

Original arguments are: red

green

yellow

blue orange

black

 

 

 

 

 

Final arguments are: orange black

$

26

Команда read

Синтаксис:

read переменная [переменная...]

Пример:

$ cat color6

#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

Please enter your favorite two colors -> red blue The colors you enterd are: blue red

$ color6

Please enter your favorite two colors -> red blue tan The colors you entered are: blue tan red

27

Команда read

Пример:

$ cat > my_install3

echo $0 will install files to your bin directory echo “Enter the names of the files -> \c”

read filenames chmod +x $filenames

mv $filenames $HOME/bin

echo Installation is complete

$ chmod +x my_install3

$ my_install3

my_install3 will install files to your bin directory Enter the names of the files -> f1 f2

Installation is complete

28

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]