標籤:des blog http ar io os 使用 sp for
1.判斷登入使用者
1.1指令碼
[[email protected]_1 shell]$ vi check_user.sh#! /bin/shecho "You are logged in as `whoami`";if [ `whoami` != devtac ]; then echo "Must be logged in as devtac to run this script." exitfiecho "Running script at `date`"
1.2運行結果
[[email protected]_1 shell]$ chmod a+x check_user.sh [[email protected]_1 shell]$ ./check_user.sh You are logged in as devtacRunning script at 2014年 12月 09日 星期二 13:35:17 CST
2.判斷是否繼續執行
2.1指令碼
[[email protected]_1 shell]$ vi do_continue.sh#! /bin/shdoContinue=necho "Do you really want to continue? (y/n)"read doContinueif [ "$doContinue" != y ]; then echo "Quitting..." exitfiecho "OK... we will continue."
2.2運行結果
[[email protected]_1 shell]$ ./do_continue.sh Do you really want to continue? (y/n)yOK... we will continue.[[email protected]_1 shell]$ ./do_continue.sh Do you really want to continue? (y/n)nQuitting...[[email protected]_1 shell]$
3 隱藏輸入
3.1 指令碼
[[email protected]_1 shell]$ vi hide_input.sh#! /bin/shstty -echoecho -n "Enter the database system password:"read pwstty echoecho "$pw was entered"
3.2 結果
./hide_input.sh Enter the database system password:123qweasd was entered[[email protected]_1 shell]$
3.3 解析
stty 命令
3.3.1 man 手冊定義
DESCRIPTION Print or change terminal characteristics.
[[email protected]_1 shell]$ stty -aspeed 38400 baud; rows 47; columns 125; line = 0;intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
本例中使用的參數
[-]echo echo input characters
屏蔽顯示
stty -echo #禁止回顯
stty echo #開啟回顯
測試方法:
stty -echo;read;stty echo;read
簡述: 使用stty -echo 的效果 就像我們輸入linux 登入密碼時,看不到輸入
4 判斷是否為目錄
4.1 指令碼
[[email protected]_1 shell]$ vi is_a_directory.sh #! /bin/shif [ -z "$1" ]; then echo "" echo "ERROR : Invalid number of arguments" echo "Usage : $0 arg1" echo "" exitfiif [ -d $1 ]; then echo "$1 is a directory."else echo "$1 is not a directory."fi
4.2 測試結果
[[email protected]_1 shell]$ ./is_a_directory.sh ERROR : Invalid number of argumentsUsage : ./is_a_directory.sh arg1[[email protected]_1 shell]$ ./is_a_directory.sh $PWD/home/devtac/shell is a directory.
4.3解析
4.3.1 指令碼傳參(未全部測試,僅作參考)
$0 指令碼名字
$1 位置參數 #1
$2 - $9 位置參數 #2 - #9
${10} 位置參數 #10
$# 位置參數的個數
"$*" 所有的位置參數(作為單個字串) *
"[email protected]" 所有的位置參數(每個都作為獨立的字串)
${#*} 傳遞到指令碼中的命令列參數的個數
${#@} 傳遞到指令碼中的命令列參數的個數
$? 傳回值
$$ 指令碼的進程ID(PID)
$- 傳遞到指令碼中的標誌(使用set)
$_ 之前命令的最後一個參數
$! 運行在背景最後一個作業的進程ID(PID)
4.3.2 判斷參數是否為空白,判讀目錄是否存在
-z string
True if the length of string is zero.
-d directory
True if the directory exists.
5.判讀檔案是否可讀
5.1指令碼
[[email protected]_1 shell]$ vi is_readable.sh#! /bin/shif [ -z "$1" ]; then echo "" echo "ERROR : Invalid number of arguments" echo "Usage : $0 agr1" echo "" exitfiif [ ! -r $1 ]; then echo "$1 is NOT readable."else echo "$1 is readable."fi~
5.2測試結果
[[email protected]_1 shell]$ ./is_readable.sh ERROR : Invalid number of argumentsUsage : ./is_readable.sh agr1[[email protected]_1 shell]$ ./is_readable.sh asdfasasdfas is NOT readable.[[email protected]_1 shell]$ ./is_readable.sh $PWD/home/devtac/shell is readable.[[email protected]_1 shell]$ ./is_readable.sh /home/devtac//home/devtac/ is readable.[[email protected]_1 shell]$ ./is_readable.sh /home/devtac/shell/is_readable.sh /home/devtac/shell/is_readable.sh is readable.
6 輸出指令碼參數
6.1 指令碼
[[email protected]_1 shell]$ vi print_args.sh#! /bin/shwhile [ $# -ne 0 ] do echo $1 shift done
6.2 輸出結果
[[email protected]_1 shell]$ ./print_args.sh [[email protected]_1 shell]$ ./print_args.sh asdf asdfasd asdfasdfasasdfasdfasdasdfasdfas[[email protected]_1 shell]$
6.3 解析
6.3.1 shift 命令
對於位置變數或命令列參數,其個數必須是確定的,或者當Shell程式不知道其個數時,可以把所有參數一起賦值給變數$*。若使用者要求Shell在不知道位置變數個數的情況下,還能逐個的把參數一一處理,也就是在$1後為$2,在$2後面為$3等。在 shift命令執行前變數$1的值在shift命令執行後就不可用了。
7複製目錄下檔案到某個目錄下
7.1指令碼
[[email protected]_1 shelltemp2]$ vi copy_special_dir_file.sh
#! /bin/sh
#echo $#
# if the number of args is not equal 2 ,output command usage and exit
if [ $# -ne 2 ];then
echo "Usage : $0 fromdir todir"
exit 1
fi
fromdir=$1 #from directory
todir=$2 #to directory
#echo $fromdir $todir
#if fromdir or todir is not a valid directory ,exit
if [ ! -d $fromdir ] || [ ! -d $todir ];then
echo $fromdir or $todir is not a valid directory
exit 1
fi
for i in $fromdir/*; do
if [ -f $i ]; then
filename=${i#$fromdir/}
echo copying $i to $todir/$filename
cp -p $i $todir/$filename
fi
done
exit 0
7.2 測試結果
[[email protected]_1 shell]$ ./copy_special_dir_file.sh /home/devtac/shell /home/devtac/shelltemp2/qewqe/home/devtac/shell or /home/devtac/shelltemp2/qewqe is not a valid directory[[email protected]_1 shell]$ rm ../shelltemp2/*[[email protected]_1 shell]$ ./copy_special_dir_file.sh /home/devtac/shell /home/devtac/shelltemp2copying /home/devtac/shell/a.txt to /home/devtac/shelltemp2/a.txtcopying /home/devtac/shell/b.txt to /home/devtac/shelltemp2/b.txtcopying /home/devtac/shell/check_user.sh to /home/devtac/shelltemp2/check_user.shcopying /home/devtac/shell/copy_special_dir_file.sh to /home/devtac/shelltemp2/copy_special_dir_file.shcopying /home/devtac/shell/copy_special_dir_file.sh.bk to /home/devtac/shelltemp2/copy_special_dir_file.sh.bkcopying /home/devtac/shell/do_continue.sh to /home/devtac/shelltemp2/do_continue.shcopying /home/devtac/shell/hide_input.sh to /home/devtac/shelltemp2/hide_input.shcopying /home/devtac/shell/is_a_directory.sh to /home/devtac/shelltemp2/is_a_directory.shcopying /home/devtac/shell/is_readable.sh to /home/devtac/shelltemp2/is_readable.shcopying /home/devtac/shell/print_args.sh to /home/devtac/shelltemp2/print_args.shcopying /home/devtac/shell/sh01.sh to /home/devtac/shelltemp2/sh01.sh[[email protected]_1 shell]$
7.3解析
7.3.1filename=${i#$fromdir/}
假設:i=/home/devtac/shell/a.txt
fromdir=/home/devtac/shell/a.txt
則:filename=a.txt
參考:來源http://linux.chinaunix.net/techdoc/develop/2007/05/05/956956.shtml
為了完整起見,我這裡再用一些例子加以說明 ${ } 的一些特異功能:
假設我們定義了一個變數為:
file=/dir1/dir2/dir3/my.file.txt
我們可以用 ${ } 分別替換獲得不同的值:
${file#*/}:拿掉第一條 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最後一條 / 及其左邊的字串:my.file.txt
${file#*.}:拿掉第一個 . 及其左邊的字串:file.txt
${file##*.}:拿掉最後一個 . 及其左邊的字串:txt
${file%/*}:拿掉最後條 / 及其右邊的字串:/dir1/dir2/dir3
${file%%/*}:拿掉第一條 / 及其右邊的字串:(空值)
${file%.*}:拿掉最後一個 . 及其右邊的字串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一個 . 及其右邊的字串:/dir1/dir2/dir3/my
記憶的方法為:
${file:0:5}:提取最左邊的 5 個位元組:/dir1
${file:5:5}:提取第 5 個位元組右邊的連續 5 個位元組:/dir2
我們也可以對變數值裡的字串作替換:
${file/dir/path}:將第一個 dir 提換為 path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:將全部 dir 提換為 path:/path1/path2/path3/my
網路參考:
http://www.ha97.com/4020.html
http://blog.csdn.net/tianlesoftware/article/details/5381984
http://blog.csdn.net/qzwujiaying/article/details/6371246
http://linux.chinaunix.net/techdoc/develop/2007/05/05/956956.shtml
書籍:
http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html
linux shell -常用指令碼