linux 特殊變量 親測

#!/bin/bash

# $$ 該shell本身的PID
printf "The complete list is %s\n" "$$"
# $! shell 最後運行的後臺的Process 的PID
printf "The complete list is %s\n" "$!"
# $? 最後運行的命令的結束代碼
printf "The complete list is %s\n" "$?"
# $- 用set命令設定的flag一覽
printf "The complete list is %s\n" "$-"
# $* 所有參數列表 以"$1 $2 … $n"的形式輸出所有參數
printf "The complete list is %s\n" "$*"
# $@ 所有參數列表 以"$1" "$2""$n" 的形式輸出所有參數。 
printf "The complete list is %s\n" "$@"
# $# 添加到 shell中的參數個數
printf "The complete list is %s\n" "$#"
# $0 shell 本身的文件名
printf "The complete list is %s\n" "$0"
# $1~$n 添加到Shell的各參數值。$1是第1參數、$2是第2參數…。
printf "The complete list is %s\n" "$1"

 以上是 shell 特殊變量的測試shell腳本,我平時工作中用的比較多的是 

$0  : 獲取當前腳本執行的路徑

#!/bin/bash
basedir=$(cd $(dirname $0); pwd)
echo "basedir: " $basedir

 

$1...$n :依次獲得腳本的參數,自不用題

$# :檢測腳本獲得了足夠多的參數

#!/bin/bash
if [ "$#" -ne 6];then
echo "Usage this.sh  <remote ip> <user> <password> <local_dir> <filename> <remote_dir>"
exit 1  
fi

 

 參考:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html

 

發佈了74 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章