標籤:shell基礎
ps: 紅字字型為重要部分, 仔細看
一、shell特性
1. history查看命令記錄,預設記錄1000條;
[[email protected] ~]# history 1 vim /etc/hosts 2 ifconfig 3 cd /etc/sysconfig/network-scripts/ifcfg- 4 cd /etc/sysconfig/network-scripts/ifcfg-eth0 5 ifconfig eth0 up……………………/省略[[email protected] ~]# vim /etc/profile #可自訂history曆史命令記錄;
2. !!執行上條命令;
[[email protected] ~]# !! 1 vim /etc/hosts 2 ifconfig 3 cd /etc/sysconfig/network-scripts/ifcfg- 4 cd /etc/sysconfig/network-scripts/ifcfg-eth0 5 ifconfig eth0 up……………………/省略
3. !$表示上條命令最後一個參數;
[[email protected] ~]# ls 1.txt1.txt[[email protected] ~]# ls !$ls 1.txt1.txt
4. alias別名;
[[email protected] ~]# alias a="ls" #建立別名;[[email protected] ~]# a1.tar 1.txt 2.txt anaconda-ks.cfg install.log install.log.syslog[[email protected] ~]# unalias a #取消別名;[[email protected] ~]# a-bash: a: command not found
5. shell中的字元;
*: 表示可以匹配零個或多個字元;
?: 可以匹配一個任一字元;
#: 表示注釋;
$: 用來標記一個變數;
~: 表示家目錄;
&: 把一條可執行命令放入後台執行;
[]: 表示裡面的括弧選一個;
6. 重新導向(輸出)、追加、輸入、錯誤重新導向、錯誤追加;
[[email protected] ~]# ls [123].txt > error.log
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/D6/wKioL1UlSfCRtxG-AAAqrsEn0f0995.jpg" title="1.png" alt="wKioL1UlSfCRtxG-AAAqrsEn0f0995.jpg" />
[[email protected] ~]# cat 2.txt >> error.log
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;background-repeat:no-repeat;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/DA/wKiom1UlSLaz8mirAAB2CqsSTFc883.jpg" title="2.png" alt="wKiom1UlSLaz8mirAAB2CqsSTFc883.jpg" />
[[email protected] ~]# cat < 2.txt
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;background-repeat:no-repeat;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/D6/wKioL1UlShazHPRVAABHeuf3sNc477.jpg" title="3.png" alt="wKioL1UlShazHPRVAABHeuf3sNc477.jpg" />
[[email protected] ~]# lasdasd 2> error_2.log
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;background-repeat:no-repeat;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5D/D6/wKioL1UlSiDSkJUZAAA8J5KKWDY913.jpg" title="4.png" alt="wKioL1UlSiDSkJUZAAA8J5KKWDY913.jpg" />
[[email protected] ~]# ndiasndias 2>> error_2.log
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/D6/wKioL1UlSinDSlPQAABbi0IROHQ767.jpg" title="5.png" alt="wKioL1UlSinDSlPQAABbi0IROHQ767.jpg" />650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;background-repeat:no-repeat;" alt="spacer.gif" />
7. 作業控制;
[[email protected] ~]# sleep 100 #按Crtl+z放到後台執行;[[email protected] ~]# sleep 200 #按Crtl+z放到後台執行; [[email protected] ~]# sleep 300 #按Crtl+z放到後台執行;[[email protected] ~]# jobs #查看後台執行作業;
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5D/D6/wKioL1UlSmbw2CZLAABYp3dIDnI765.jpg" title="6.png" alt="wKioL1UlSmbw2CZLAABYp3dIDnI765.jpg" />
[[email protected] ~]# fg 1 #將作業調回到前台執行;
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/DA/wKiom1UlSSWRmYS4AAAfr0AalTM296.jpg" title="7.png" alt="wKiom1UlSSWRmYS4AAAfr0AalTM296.jpg" />650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="border:1px solid rgb(221,221,221);background-position:50% 50%;background-repeat:no-repeat;" alt="spacer.gif" />
二、變數
1. 自訂變數;
[[email protected] ~]# a=c[[email protected] ~]# set | grep -n ^a #set可以把所有變數列出來;55:a=c[[email protected] ~]# env | grep ^a #env可以列出的目前使用者的所有環境變數;[[email protected] ~]# export a=AAAA #export引入全域變數;[[email protected] ~]# bash[[email protected] ~]# echo $aAAAA[[email protected] ~]# env | grep -n ^a12:a=AAAA[[email protected] ~]# exitexit[[email protected] ~]# echo $aAAAA
650) this.width=650;" src="/e/u261/themes/default/images/spacer.gif" style="background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd;" alt="spacer.gif" />650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5D/DA/wKiom1UlSS6gGR1LAADVI9cSABQ013.jpg" title="8.png" alt="wKiom1UlSS6gGR1LAADVI9cSABQ013.jpg" />
[[email protected] ~]# bash #取消一個自訂變數;[[email protected] ~]# unset a[[email protected] ~]# env | grep ^a
2. 自訂變數中間如果帶有空格, 需用引號;
[[email protected] ~]# a=aming Linux-bash: Linux: command not found[[email protected] ~]# a=‘aming Linux‘[[email protected] ~]# echo $aaming Linux[[email protected] ~]# b=`echo $a` #` `引用裡面的值賦值給b;[[email protected] ~]# echo $baming Linux[[email protected] ~]# b=‘echo $a‘ #‘ ‘只列印出裡面的參數;[[email protected] ~]# echo $becho $a[[email protected] ~]# b="echo $a" #" "引用裡面的值並列印;[[email protected] ~]# echo $becho aming Linux
3. 合并變數;
[[email protected] ~]# a=2[[email protected] ~]# b=1[[email protected] ~]# c=$a‘$b‘[[email protected] ~]# echo $c2$b[[email protected] ~]# c=$a"$b"[[email protected] ~]# echo $c21[[email protected] ~]# c=$a`$b`-bash: 1: command not found
4. 系統變數和使用者變數;
[[email protected] ~]# cat /etc/profile[[email protected] ~]# cat /etc/bashrc[[email protected] ~]# cat .bash_profile[[email protected] ~]# cat .bashrc [[email protected] ~]# echo ‘echo "bashrc"‘ >> .bashrc [[email protected] ~]# echo ‘echo "profile"‘ >> .bash_profile[[email protected] ~]# bashbashrc[[email protected] ~]# su -bashrcprofile
總結:
/etc/profile和/etc/bashrc屬於全域配置.
$HOME/bashrc和$HOME/bash_profile屬於使用者配置.
一組對所有使用者生效, 一組對目前使用者生效.
三、Shell常用命令
1. cut分割
-d: 指定分隔字元;
-f: 列印第幾段;
-c: 指定截取字元;
以‘:‘為分隔字元, 列印/etc/passwd第一段到三段的字元;[[email protected] ~]# cut -d ‘:‘ -f 1-3 /etc/passwd | head -3root:x:0bin:x:1daemon:x:2截取/etc/passwd的第一個字元到第五個字元;[[email protected] ~]# cut -c 1-5 /etc/passwd | head -3root:bin:xdaemo
2. sort排序
-t: 指定分隔字元;
-k: 對第幾列排序;
-n: 以數字排序(預設從小到大);
-r: 反向排序,結合-n使用;
-u: 去掉重複行;
以‘:‘為分隔字元, 以從小到大數字排序/etc/passwd的第三段字元;[[email protected] ~]# sort -t ‘:‘ -k3 -n /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin……………………/省略以‘:‘為分隔字元, 以從大到小數字排序/etc/passwd的第三段字元;[[email protected] ~]# sort -t ‘:‘ -k 3 -nr /etc/passwd user1:x:500:500::/home/user1:/bin/bashsaslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologindhcpd:x:177:177:DHCP server:/:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologin
3. uniq去重行
-c: 去重行;
[[email protected] ~]# cat 2.txt222222333222333111222[[email protected] ~]# uniq -c 2.txt 2 222 1 333 1 222 1 333 1 111 1 222
4. wc統計行數、字元數、詞數
-l: 統計行數;
-m: 統計字元數;
-w: 統計詞數;
[[email protected] ~]# wc 2.txt7 7 28 2.txt[[email protected] ~]# wc -l 2.txt7 2.txt[[email protected] ~]# wc -m 2.txt28 2.txt[[email protected] ~]# wc -w 2.txt7 2.txt
5. tr替換字元
[[email protected] ~]# ls | tr ‘1-9‘ ‘A-Z‘ #將數字1-9替換為A-Z;
6. split切割檔案
-b: 按檔案大小分割;
-l : 按行數分割;
[[email protected] ~]# du -sh 1.txt 160K 1.txt[[email protected] ~]# split -l 20 1.txt
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5D/D6/wKioL1UlSqDCsjSXAAKZwRgSyYg966.jpg" title="9.png" alt="wKioL1UlSqDCsjSXAAKZwRgSyYg966.jpg" />
[[email protected] ~]# ls x* | xargs -i mv {} {}.txt #將x*改為x*.txt[[email protected] ~]# split -l 20 1.txt log #自訂分割完後的名字;
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5D/D6/wKioL1UlSq6TTXkLAAdflaT7q-8593.jpg" title="10.png" alt="wKioL1UlSq6TTXkLAAdflaT7q-8593.jpg" />
7. &&、||、;;
&&: 前面命令執行成功後執行後面命令;
[[email protected] ~]# ls 1.txt && cd /root1.txt[[email protected] ~]# ls aaaa.txt && cd /homels: cannot access aaaa.txt: No such file or directory||: 前面命令執行不成功執行後面;[[email protected] home]# ls aaaa.txt || cd /root/ls: cannot access aaaa.txt: No such file or directory;: 前面命令是否執行完成都會執行後面命令;[[email protected] ~]# ss ; cd /tmp
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5D/D6/wKioL1UlSxfRCABXAACUVT0SJX4813.jpg" title="·11.png" alt="wKioL1UlSxfRCABXAACUVT0SJX4813.jpg" />
本文出自 “陳小賤。” 部落格,請務必保留此出處http://chenxiaojian.blog.51cto.com/9345444/1630205
2015/04/08 Shell基礎-1