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

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.
主站蜘蛛池模板: 梁平县| 轮台县| 甘洛县| 开封市| 剑川县| 成武县| 会泽县| 保定市| 湖州市| 嘉祥县| 白沙| 清河县| 安化县| 华安县| 石门县| 聂拉木县| 巴彦县| 福安市| 江西省| 抚顺市| 浠水县| 崇阳县| 寻乌县| 兴国县| 黑山县| 哈巴河县| 库尔勒市| 卢氏县| 昌都县| 德昌县| 云霄县| 景宁| 西林县| 如东县| 乌恰县| 伊川县| 曲阳县| 威海市| 洛南县| 泰宁县| 湟中县|