偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

3 個實例介紹 shell 腳本中幾個特殊參數(shù)的用法

系統(tǒng) Linux
在本文中討論的一些shell特殊參數(shù)是:$*,$@,$#,$$,$!

[[402692]]

在本文中討論的一些shell特殊參數(shù)是:$*,$@,$#,$$,$!

示例1:使用 $*和$@ 來擴展位置參數(shù)

本實例腳本中使用$*和$@參數(shù): 

  1. [root@localhost scripts]# vim expan.sh  
  2. #!/bin/bash  
  3. export IFS='-'  
  4. cnt=1  
  5. # Printing the data available in $*  
  6. echo "Values of \"\$*\":"  
  7. for arg in "$*"  
  8. do  
  9.   echo "Arg #$cnt= $arg"  
  10.   let "cnt+=1"  
  11. done  
  12. cnt=1  
  13. # Printing the data available in $@  
  14. echo "Values of \"\$@\":"  
  15. for arg in "$@"  
  16. do  
  17.   echo "Arg #$cnt= $arg"  
  18.   let "cnt+=1"  
  19. done 

下面是運行結(jié)果: 

  1. [root@localhost scripts]# ./expan.sh "Hello world" 2 3 4  
  2. Values of "$*":  
  3. Arg #1Hello world-2-3-4  
  4. Values of "$@":  
  5. Arg #1Hello world  
  6. Arg #22= 2  
  7. Arg #33= 3  
  8. Arg #44= 4 

export IFS='-'表示使用" - "表示內(nèi)部字段分隔符。

當打印參數(shù)$*的每個值時,它只給出一個值,即是IFS分隔的整個位置參數(shù)。

而$@將每個參數(shù)作為單獨的值提供。

示例2:使用$#統(tǒng)計位置參數(shù)的數(shù)量

$#是特殊參數(shù),它可以提更腳本的位置參數(shù)的數(shù)量: 

  1. [root@localhost scripts]# vim count.sh  
  2. #!/bin/bash  
  3. if [ $# -lt 2 ]  
  4. then  
  5.   echo "Usage: $0 arg1 arg2"  
  6.   exit  
  7. fi  
  8. echo -e  "\$1=$1"  
  9. echo -e "\$2=$2"  
  10. let add=$1+$2  
  11. let sub=$1-$2  
  12. let mul=$1*$2  
  13. let div=$1/$2  
  14. echo -e "Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n" 

下面是運行結(jié)果: 

  1. [root@localhost scripts]# ./count.sh   
  2. Usage: ./count.sh arg1 arg2  
  3. [root@localhost scripts]# ./count.sh 2314 15241  
  4. $1=2314  
  5. $2=15241  
  6. Addition=17555  
  7. Subtraction=-12927  
  8. Multiplication=35267674  
  9. Division=0 

腳本中if [ $# -lt 2 ]表示如果位置參數(shù)的數(shù)量小于2,則會提示"Usage: ./count.sh arg1 arg2"。

示例3:與過程相關(guān)的參數(shù) $$和$!

參數(shù)$$將給出shell腳本的進程ID。$!提供最近執(zhí)行的后臺進程的ID,下面實例是打印當前腳本的進程ID和最后一次執(zhí)行后臺進程的ID: 

  1. [root@localhost scripts]# vim proc.sh  
  2. #!/bin/bash  
  3. echo -e "Process ID=$$"  
  4. sleep 1000 &  
  5. echo -e "Background Process ID=$!" 

下面是執(zhí)行的結(jié)果: 

  1. [root@localhost scripts]# ./proc.sh   
  2. Process ID=14625  
  3. Background Process ID=14626  
  4. [root@localhost scripts]# ps  
  5.    PID TTY          TIME CMD  
  6.   3665 pts/0    00:00:00 bash  
  7.  14626 pts/0    00:00:00 sleep  
  8.  14627 pts/0    00:00:00 ps 

 

 

責任編輯:龐桂玉 來源: 良許Linux
相關(guān)推薦

2019-08-09 13:50:08

shellLinux

2021-05-11 07:50:31

BashShell腳本

2019-08-02 09:13:22

Linux腳本語言歡聚時代

2021-05-12 10:17:15

Shell工具Linux

2021-04-11 07:56:42

ShellLinux

2019-10-31 08:22:39

shell腳本Linux

2021-04-15 11:21:26

Shell腳本Linux

2019-08-13 11:53:01

腳本語言AWKBash

2024-11-12 12:19:39

2023-11-13 22:08:05

ShellLinux

2010-11-24 15:53:11

MySQL中SELEC

2010-07-01 14:25:31

UML時序圖

2021-06-07 13:02:31

Shell腳本Linux

2010-09-10 13:25:22

2022-07-21 14:38:17

PythonShell

2010-03-26 15:28:05

Python編寫

2019-08-12 07:45:44

Linux腳本shell

2018-08-28 16:02:59

LinuxShellBash

2017-03-08 11:10:39

LinuxShell命令

2021-05-06 15:18:09

Shell腳本Linux
點贊
收藏

51CTO技術(shù)棧公眾號