- Learning Linux Shell Scripting
- Ganesh Naik
- 316字
- 2021-06-25 22:02:56
Understanding shift
Using shift, we can change the parameter to which $1 and $2 are pointing to the next variable.
Create script shift_01.sh, as follows:
#!/bin/bash echo "All Arguments Passed are as follow : " echo $* echo "Shift By one Position :" shift echo "Value of Positional Parameter $ 1 after shift :" echo $1 echo "Shift by Two Positions :" shift 2 echo "Value of Positional Parameter $ 1 After two Shifts :" echo $1
Execute the following command:
$ chmod +x shift_01.sh$ ./shift_01.sh One Two Three Four
The output is as follows:
[student@localhost ~]$ ./shift_01.sh One Two Three Four
All arguments passed are as follows:
One Two Three Four
Shift by one position.
Here, the value of the positional parameter $1 after shift is:
Two
Shift by two positions.
The value of the positional parameter $1 after two shifts:
Four
We can see that, initially, $1 was One. After the shift, $1 will be pointing to Two. Once the shift has been done, the value in position 1 is always destroyed and is inaccessible.
Create script shift_02.sh, as follows:
#!/bin/bash echo '$#: ' $# echo '$@: ' $@ echo '$*: ' $* echo echo '$1 $2 $9 $10 are: ' $1 $2 $9 $10 echo shift echo '$#: ' $# echo '$@: ' $@ echo '$*: ' $* echo echo '$1 $2 $9 are: ' $1 $2 $9 shift 2 echo '$#: ' $# echo '$@: ' $@ echo '$*: ' $* echo echo '$1 $2 $9 are: ' $1 $2 $9 echo '${10}: ' ${10}
From this script's execution, the following output is shown:
- Initially, $1 to $13 were numerical values 1 to 13, respectively.
- When we called the command shift, it then$1 shifted to number 2, and all $numbers were shifted accordingly.
- When we called the command shift 2, then $1 shifted to number 4 and all $numbers were shifted accordingly.
推薦閱讀
- 高性能混合信號ARM:ADuC7xxx原理與應(yīng)用開發(fā)
- 軟件架構(gòu)設(shè)計
- 計算機系統(tǒng)結(jié)構(gòu)
- 分?jǐn)?shù)階系統(tǒng)分析與控制研究
- 貫通Java Web開發(fā)三劍客
- 空間站多臂機器人運動控制研究
- 分析力!專業(yè)Excel的制作與分析實用法則
- 智能生產(chǎn)線的重構(gòu)方法
- 云計算和大數(shù)據(jù)的應(yīng)用
- Visual Studio 2010 (C#) Windows數(shù)據(jù)庫項目開發(fā)
- 精通LabVIEW程序設(shè)計
- 基于ARM9的小型機器人制作
- Mastering Predictive Analytics with scikit:learn and TensorFlow
- Hands-On DevOps
- 從機器學(xué)習(xí)到無人駕駛