- Learning Linux Shell Scripting
- Ganesh Naik
- 353字
- 2021-06-25 22:02:56
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:
- Initially, when the set is not called, then $1, $2, and $3 do not contain any information.
- Then, we set $1 to $3 as GERMAN numerals in words.
- Then, we set $1 to $5 as the name, phone number, address, date of birth, and salary, respectively.
推薦閱讀
- PPT,要你好看
- 繪制進程圖:可視化D++語言(第1冊)
- 軟件架構(gòu)設(shè)計
- 西門子PLC與InTouch綜合應(yīng)用
- 精通Excel VBA
- Arduino &樂高創(chuàng)意機器人制作教程
- 自動控制理論(非自動化專業(yè))
- Machine Learning with Apache Spark Quick Start Guide
- 網(wǎng)中之我:何明升網(wǎng)絡(luò)社會論稿
- PVCBOT機器人控制技術(shù)入門
- Windows Server 2008 R2活動目錄內(nèi)幕
- Statistics for Data Science
- Word 2007,Excel 2007辦公應(yīng)用融會貫通
- 奇點將至
- 基于ARM9的小型機器人制作