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

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.
主站蜘蛛池模板: 泽库县| 濉溪县| 抚宁县| 商南县| 乳源| 蒙自县| 筠连县| 仁寿县| 英山县| 淄博市| 山西省| 民勤县| 湘乡市| 南宫市| 民勤县| 姜堰市| 峨山| 沾化县| 绥中县| 麻江县| 庐江县| 华宁县| 平山县| 余江县| 邛崃市| 石泉县| 肃北| 阳曲县| 纳雍县| 郁南县| 津南区| 九台市| 奉化市| 获嘉县| 即墨市| 凌云县| 盐边县| 定远县| 苏尼特右旗| 噶尔县| 涪陵区|