shell編程基礎(三): 位置參數與shell腳本的輸入輸出


一、位置參數和特殊變量

有很多特殊變量是被Shell自動賦值的,我們已經遇到了$?和$1,現在總結一下:

常用的位置參數和特殊變量:

$0  相當於C語言main函數的argv[0]
$1、$2...    這些稱為位置參數(Positional Parameter),相當於C語言main函數的argv[1]、argv[2]...
$#  相當於C語言main函數的argc - 1,表示輸入參數的個數,注意這里的#后面不表示注釋
$@  表示參數列表"$1" "$2" ...,例如可以用在for循環中的in后面。
$*  表示參數列表"$1" "$2" ...,同上
$?  上一條命令的Exit Status
$$  當前進程號

位置參數可以用shift命令左移。比如shift 3表示原來的$4現在變成$1,原來的$5現在變成$2等等,原來的$1、$2、$3丟棄,$0不移動。不帶參數的shift命令相當於shift 1。例如:

[root@VM_0_5_centos test]# vi tsite.sh
查看腳本內容
[root@VM_0_5_centos test]# cat tsite.sh 
#!/bin/sh
echo '$0相當於C語言main函數的argv[0]'
echo "The program $0 is now running"
echo '-------------------------'
echo "The first parameter is --> $1"
echo "The second parameter is --> $2"
echo '-------------------------'
echo "The parameter list is --> $@"
echo '-------------------------'
echo 'shift命令默認左移一位'
shift
echo "The first parameter is --> $1"
echo "The second parameter is --> $2"
echo "The parameter list is --> $@"
echo '-------------------------'
echo "當前進程號 --> $$"
echo "當前輸入參數個數 --> $#"
提升腳本權限
[root@VM_0_5_centos test]# chmod a+w tsite.sh 
[root@VM_0_5_centos test]# ls
$ $  mmzs  tsite.sh
運行測試腳本
[root@VM_0_5_centos test]# sh tsite.sh aa bb cc dd ee
$0相當於C語言main函數的argv[0]
The program tsite.sh is now running
-------------------------
The first parameter is --> aa
The second parameter is --> bb
-------------------------
The parameter list is --> aa bb cc dd ee
-------------------------
shift命令默認左移一位
The first parameter is --> bb
The second parameter is --> cc
The parameter list is --> bb cc dd ee
-------------------------
當前進程號 --> 6871
當前輸入參數個數 --> 4

二、shell輸入輸出

1、echo

echo顯示文本行或變量,或者把字符串輸入到文件。

echo [option] string
-e 解析轉義字符
-n 不回車換行。默認情況echo回顯的內容后面跟一個回車換行。

[root@VM_0_5_centos test]# vi techo.sh
查看腳本內容
[root@VM_0_5_centos test]# cat techo.sh 
#!/bin/sh
echo "hello\n\n"
echo -e "hello\n\n"
echo  "hello"
echo -n "hello"
運行測試腳本
[root@VM_0_5_centos test]# sh techo.sh 
hello\n\n
hello


hello
hello[root@VM_0_5_centos test]# 

2、管道|

可以通過管道把一個命令的輸出傳遞給另一個命令做輸入。管道用豎線表示。

[root@VM_0_5_centos test]# cat techo.sh | more
#!/bin/sh
echo "hello\n\n"
echo -e "hello\n\n"
echo  "hello"
echo -n "hello"
[root@VM_0_5_centos test]# ls -l | grep "t"
total 12
-rw-r--r-- 1 root root    0 Jul 12 14:50 $ $
drwxr-xr-x 2 root root 4096 Jul 12 15:26 mmzs
-rw-r--r-- 1 root root   77 Jul 13 11:24 techo.sh
-rw-rw-rw- 1 root root  559 Jul 13 10:54 tsite.sh
df -k | awk '{print $1}' | grep -v "文件系統"
df -k 查看磁盤空間,找到第一列,去除“文件系統”,並輸出
[root@VM_0_5_centos test]# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 51474024 8321872 40829692 17% / devtmpfs 932328 0 932328 0% /dev tmpfs 941808 24 941784 1% /dev/shm tmpfs 941808 556 941252 1% /run tmpfs 941808 0 941808 0% /sys/fs/cgroup tmpfs 188364 0 188364 0% /run/user/0

3、tee

tee命令把結果輸出到標准輸出,另一個副本輸出到相應文件。

df -k | awk '{print $1}' | grep -v "文件系統" | tee a.txt
tee -a a.txt表示追加操作。
df -k | awk '{print $1}' | grep -v "文件系統" | tee -a a.txt
//將ls -l命令的結果輸出到a.txt文件中
[root@VM_0_5_centos test]# ls -l | tee a.txt

4、文件重定向

概念理解:

cmd表示輸入的命令
cmd > file           把標准輸出重定向到新文件中
cmd >> file          追加
cmd > file 2>&1      標准出錯2也重定向到標准輸出1所指向的標准輸出的file里
cmd >> file 2>&1

cmd < &fd    把文件描述符fd作為標准輸入
cmd > &fd    把文件描述符fd作為標准輸出 cmd < &- 關閉標准輸入
例如:
cat 1.txt  相當於open,直接打開文件讀取內容
cat < 1.txt 此時cat是去讀標准輸入,但是標准輸入指向1.txt文件,所以cat就去讀1.txt文件了

cmd < file1 > file2   輸入定向到file1,輸出定向到file2文件里

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM