官术网_书友最值得收藏!

Understanding set

Many times, we may not pass arguments to the command line, but we may need to set parameters internally inside the script.

We can declare parameters with the set command, as follows:

$ set USA Canada UK France$ echo $1USA$ echo $2Canada$ echo $3UK$ echo $4France

We can use this inside the set_01.sh script, as follows:

#!/bin/bash 
set USA Canada UK France 
echo $1 
echo $2 
echo $3 
echo $4 

Run the script as this:

$ ./set.sh

The output is as follows:

USACanadaUKFrance

Following is a summary of the declare options:

Type in the following commands:

set One Two Three Four Five
echo $0 # This will show command
echo $1 # This will show first parameter
echo $2echo $* # This will list all parameters
echo $# # This will list total number of parameters
echo ${10} ${11} # Use this syntax for parameters for 10th and # 11th parameters

Let's write script set_02.sh, as follows:

#!/bin/bash 
echo The date is $(date) 
set $(date) 
echo The month is $2 
exit 0 

The output is as follows:

In the script $(date), the command will execute, and the output of that command will be used as $1, $2, $3, and so on. We have used $2 to extract the month from the output.

Let's write script set_03.sh, as follows:

#!/bin/bash 
 
echo "Executing script $0" 
echo $1 $2 $3 
 
set eins zwei drei 
echo "One two three in German are:" 
echo "$1" 
echo  "$2" 
echo  "$3" 
 
textline="name phone address birthdate salary" 
set $textline 
echo  "$*" 
echo 'At this time $1 = '$1' and $4 = '$4'' 

The output is as follows:

Executing script ./hello.sh 
 
One two three in German are: 
eins 
zwei 
drei 
 
name phone address birthdate salary 
At this time $1 = name and $4 = birthdate

In this script, the output shows:

  1. Initially, when the set is not called, then $1, $2, and $3 do not contain any information.
  2. Then, we set $1 to $3 as GERMAN numerals in words.
  3. Then, we set $1 to $5 as the name, phone number, address, date of birth, and salary, respectively.
主站蜘蛛池模板: 罗平县| 禹州市| 伊吾县| 大英县| 甘德县| 行唐县| 乌审旗| 滦平县| 巢湖市| 商都县| 托里县| 拉萨市| 中江县| 永丰县| 同江市| 乳源| 德江县| 利川市| 绥滨县| 申扎县| 壶关县| 惠来县| 泊头市| 广安市| 托克托县| 恩平市| 仪征市| 竹山县| 横山县| 闵行区| 西安市| 石楼县| 永康市| 会昌县| 浏阳市| 牟定县| 庆阳市| 嘉峪关市| 萨迦县| 沾益县| 东安县|