溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

shell編程基礎(chǔ)(一)

發(fā)布時(shí)間:2020-07-14 02:30:01 來(lái)源:網(wǎng)絡(luò) 閱讀:537 作者:shw23 欄目:開(kāi)發(fā)技術(shù)

一、命令行

命令行結(jié)構(gòu):命令名、命令選項(xiàng)和命令參數(shù)三部分內(nèi)容組成,中間以空格或制表符等空白字符分隔。如下:
 命令名  命令選項(xiàng)  命令參數(shù)
command [option] [parameter]

命令選項(xiàng)通常以減號(hào)“-”開(kāi)始的單個(gè)字符,主要用于限定命令的具體功能,同時(shí)也決定命令的最終運(yùn)行結(jié)果
 選項(xiàng)可以單獨(dú)給出,也可以組合使用。如果選項(xiàng)后面有參數(shù),選項(xiàng)與參數(shù)必須單獨(dú)列出。

命令選項(xiàng)可以省略,命令參數(shù)也可以省略,命令名必須提供。
 命令行只包含命令名時(shí),命令選項(xiàng)和參數(shù)采用默認(rèn)值。

命令行一次輸入多個(gè)命令時(shí),命令之間使用分號(hào);分隔

[root@mrhcatxq01 ~]# date     #命令行只有命令名,選項(xiàng)和參數(shù)默認(rèn)值
Thu Mar 24 16:14:38 CST 2016
 [root@mrhcatxq01 install_cacti]# date "+%Y%m%d"
 20160324
 [root@mrhcatxq01 ~]# uname -a     #命令行有命令名、命令選項(xiàng)
Linux mrhcatxq01 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
 [root@mrhcatxq01 ~]# ls /etc/passwd     #命令行有命令名、命令參數(shù)
/etc/passwd
 [root@mrhcatxq01 ~]# ls -l /etc/passwd     #命令行有命令名、命令選項(xiàng)、命令參數(shù)
-rw-r--r--. 1 root root 1800 Feb 22 02:18 /etc/passwd
 [root@mrhcatxq01 ~]# cd /install_cacti/
 [root@mrhcatxq01 install_cacti]# ls -la     #命令選項(xiàng)組合使用
total 8464
 drwxrwxrwx.  3 root root    4096 Mar 24 01:00 .
 dr-xr-xr-x. 29 root root    4096 Mar 10 03:39 ..
 -rw-r--r--.  1 root root 2594409 Feb 22 01:43 cacti-0.8.8f.tar.gz
 -rw-r--r--   1 root root  401737 Mar 24 01:00 cacti_backup_20160324.sql
 -rwxr-xr-x   1 root root     326 Mar 21 13:58 cacti_backup.sh
 [root@mrhcatxq01 install_cacti]#
 [root@mrhcatxq01 install_cacti]# sort -t :  -k 4 -n -r /etc/passwd     #選項(xiàng)后有參數(shù),必須單獨(dú)列出
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
 cacti:x:501:501::/home/cacti:/bin/bash
 tomcat:x:500:500::/home/tomcat:/bin/bash
 rtkit:x:499:496:RealtimeKit:/proc:/sbin/nologin
 pulse:x:497:494:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
 abrt:x:173:173::/etc/abrt:/sbin/nologin

 

二、重定向

> 輸出重定向(覆蓋)  command > outputfile 文件存在,則覆蓋原來(lái)的內(nèi)容,否則創(chuàng)建文件
>> 輸出重定向(追加) command >> outputfile 文件存在,則追加新內(nèi)容至文件末尾,否則創(chuàng)建文件
< 輸入重定向   command < inputfile 讀取文件內(nèi)容作為輸入數(shù)據(jù)

標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出重定向到不同文件:(1表示標(biāo)準(zhǔn)輸出描述符;2表示標(biāo)準(zhǔn)錯(cuò)誤輸出描述符)
command > standard.output 2> standard.error 或 command 1> standard.output 2> standard.error

標(biāo)準(zhǔn)錯(cuò)誤輸出重定向到標(biāo)準(zhǔn)輸出:
command &> output 或 command > output 2>&1    (2>&表示 標(biāo)準(zhǔn)錯(cuò)誤輸出2重定向>到后面指定的文件描述符,這里文件描述符是1,即標(biāo)準(zhǔn)輸出)

i>&j 把文件描述符i表示的輸出重定向到文件描述符j表示的重定向文件。
 常用:command > /dev/null 2>&1

 

三、管道

管道:前一命令的輸出作為后一命令的輸入   command1 | command2

[root@u1acatap01 configs]# ls -lrt
 total 88
 -rw-rw-r-- 1 apache cacti    59 May 24 17:33 index.php
 -rw-rw-r-- 1 apache cacti  1267 May 30 01:52 simple.conf
 -rw-rw-r-- 1 apache cacti 15269 Jun  3 16:57 global_prod.confbak
 -rw-rw-r-- 1 apache cacti 16660 Jun  7 11:34 global_prod.conf
 -rw-rw-r-- 1 apache cacti 10342 Jun  7 15:07 final_prod.conf
 -rw-r--r-- 1 root   root  10342 Jun  7 15:11 final_prod.confbak22
 -rw-r--r-- 1 root   root  16660 Jun  7 15:12 global_prod.confbak22
 [root@u1acatap01 configs]# ls -lrt|grep final_prod.conf
 -rw-rw-r-- 1 apache cacti 10342 Jun  7 15:07 final_prod.conf
 -rw-r--r-- 1 root   root  10342 Jun  7 15:11 final_prod.confbak22
 [root@u1acatap01 configs]#


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI