bash的常用功能
檢視曆史指令:#history
[root@localhost ~]# history
1 ifconfig
2 exit
3 ifconfig
4 system-config-service
5 system-config-services
6 ifconfig
7 ifup eth0
8 service network restart
9 ifconfig
10 ifup eth0
11 service network restart
12 ifconfig
13 service network restart
14 vim /etc/sysconfig/network-scripts/ifcfg-eth0
15 vim /etc/udev/rules.d/70-persistent-net.rules
16 reboot
17 exit
18 vi /etc/sysconfig/network-scripts/ifcfg-eth0
19 service network restart
想要執行某個曆史指令可以使用:#!n 比如199 #!199
28 cd
29 ll
30 history
[root@localhost ~]# !29 例如29 ll 指令
ll
總用量 96
-rw-------. 1 root root 1606 2月 26 22:36 anaconda-ks.cfg
-rw-r--r--. 1 root root 44311 2月 26 22:36 install.log
-rw-r--r--. 1 root root 10727 2月 26 22:34 install.log.syslog
drwxr-xr-x. 2 root root 4096 2月 26 22:39 公共的
drwxr-xr-x. 2 root root 4096 2月 26 22:39 模闆
drwxr-xr-x. 2 root root 4096 2月 26 22:39 視訊
drwxr-xr-x. 2 root root 4096 2月 26 22:39 圖檔
drwxr-xr-x. 2 root root 4096 2月 26 22:39 文檔
drwxr-xr-x. 2 root root 4096 2月 26 22:39 下載下傳
drwxr-xr-x. 2 root root 4096 2月 26 22:39 音樂
drwxr-xr-x. 2 root root 4096 2月 26 22:39 桌面
!str:執行曆史記錄中以“str”開頭的指令,設定記錄曆史指令的條數
[root@localhost ~]# !l 它會執行離本次指令最近的l指令——》ll
[root@localhost ~]#
清除曆史指令:#history -c
[root@localhost ~]# history -c
[root@localhost ~]# history 清除完成後會看到曆史指令為空
1 history
[root@localhost ~]#
#alias 檢視指令的别名 我們也可以自己設定比如:
[root@localhost ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
#alias test="ls -l"
[root@localhost ~]# alias test='ls -l'
[root@localhost ~]# test
[root@localhost ~]# unalias test
[root@localhost ~]# alias test=ls -l
-bash: alias: -l: not found
[root@localhost ~]# alias test="ls -l"
#test 也就等于敲了ls -l 檢視指令
#unalias test 取消test="ls -l"設定
> 輸出 比如ll > one.txt >後面要養成留白格的習慣> one.txt,不留白格有時候會報錯,系統識别不了
< 輸入 比如cat <one.txt
>> 将指令執行的結果追加輸出到指定檔案
2> 清空指定檔案的内容,并将标準錯誤資訊儲存到該檔案
2>> 将标準的錯誤資訊追加輸出到指定的檔案中
&> 将标準輸出,标準錯誤輸出的内容全部儲存到指定檔案,而不是直接顯示在螢幕上
0 标準輸入 一般從鍵盤輸入
1 标準的輸出 正确的輸出
2 錯誤的輸出
前提是我們先寫一個shell腳本
# ls -l &&1111111&>one2.txt
[root@localhost ~]# ./a.sh &>one
[root@localhost ~]# cat one
anaconda-ks.cfg
a.sh
install.log
install.log.syslog
one
公共的
模闆
視訊
圖檔
文檔
下載下傳
音樂
桌面
./a.sh: line 4: 111111111111111: command not found
[root@localhost ~]# cat a.sh
#!/bin/bash
#a.sh
ls
111111111111111
[root@localhost ~
shell變量的應用
#a=100
[root@localhost ~]# a=100
[root@localhost ~]# echo $a
100
#set 檢視系統中所有的變量 局部變量和全局變量
[root@localhost ~]# set | more
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_she
ll:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=111
CVS_RSH=ssh
DIRSTACK=()
DISPLAY=localhost:10.0
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
#env 專門檢視全局變量
[root@localhost ~]# env
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.0.124 1075 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib/qt-3.3
QTINC=/usr/lib/qt-3.3/include
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*
變量的指派與運算
#echo $a 輸出變量a的值
#expr $a + $a 計算a+a的值
#expr $a \* $a 計算a*a的值 \屏蔽特殊字元的意義
[root@localhost ~]# expr $a + $a
200
[root@localhost ~]# expr $a \* $a
10000
[root@localhost ~]# expr 2 % 1 取餘
You have mail in /var/spool/mail/root
[root@localhost ~]# expr 2 / 1
2
雙引号&ldquo;&rdquo;:允許通過$符号引用其他變量值
單引号&lsquo;&rsquo;:禁止引用其他變量值,$視為普通字元
反撇号``:将指令執行的結果輸出給變量
#echo &ldquo;aaaaaaaaaaaaaaaaaaaaa&rdquo;
#echo &ldquo;aaaaaaa $a aaaaaaaaa&rdquo;
#echo &lsquo;aaaaaaa $a aaaaaaaaa&rsquo;
#echo &ldquo;aaaaaaaaaa$aaaaaaaaaaa&rdquo;
[root@localhost ~]# echo "aaaaaaaaaaaaaaa"
aaaaaaaaaaaaaaa
[root@localhost ~]# echo "aaaaaaaaa $a aaaaa"
aaaaaaaaa 100 aaaaa
[root@localhost ~]# echo 'aaaaaaaaa $a aaaaa'
aaaaaaaaa $a aaaaa
[root@localhost ~]# echo 'aaaaaaaaa$aaaaaa'
aaaaaaaaa$aaaaaa
[root@localhost ~]# echo "aaaaaaaaa$aaaaaa" 忽略後面$aaaaaaa的變量,沒找到比對的變量
aaaaaaaaa
[root@localhost ~]# echo "aaaaaa${a}aaaaaaaaaa"
aaaaaa100aaaaaaaaaa
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
hgfs
[root@localhost mnt]# echo `ll`
總用量 4 drwxr-xr-x. 2 root root 4096 2月 27 00:58 hgfs
[root@localhost mnt]#
str="this is a boy"
a=11111111111111111111$str
b=222222222222222222222$str
c=33333333333333333333$str
echo $a
echo $b
echo $c
[root@localhost ~]# ./a.sh
11111111111111111111this is a boy
222222222222222222222this is a boy
33333333333333333333this is a boy
[root@localhost ~]# vi a.sh
11111111111111111111this is a test
222222222222222222222this is a test
33333333333333333333this is a test
str="this is a test"
怎麼把局部變量變成全局變量:export
[root@localhost ~]# www=1000
[root@localhost ~]# echo $www
1000
[root@localhost ~]# vi a.sh
[root@localhost ~]# cat a.sh
echo $www
[root@localhost ~]# export www
[root@localhost ~]# ./a.sh
HISTSIZE=100
SSH_CLIENT=192.168.0.131 2752 22
SSH_TTY=/dev/pts/1
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/root
PATH=/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
www=1000
LOGNAME=root
QTLIB=/usr/lib/qt-3.3/lib
SSH_CONNECTION=192.168.0.131 2752 192.168.0.1 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
_=/bin/env
清除變量
[root@localhost ~]# unset www
[root@localhost ~]# echo $www
位置變量
表示為$n,n為1~9之間的數字
案例:
echo $1 ●位置變量1
echo $2 ●位置變量2
echo $3 ●位置變量3
echo $# ●位置變量的個數
echo $* ●位置變量的内容
echo $0 ●位置變量的程式名
[root@localhost ~]# ./a.sh a b c d e 執行此腳本
a
b
c
5
a b c d e
./a.sh
此案例針對$?,如果指令輸出正确,$?輸出的值将是0,如果執行錯誤輸出的值将是非0
ls >> /dev/null /dev/null先相當于windows的垃圾箱,但是他是個無底洞,垃圾歸檔到這
echo $?
echo "##########" 不論輸出正确與否我們都将輸出結果歸檔到這
1111111111 >> /dev/null
##########
./a.sh: line 6: 1111111111: command not found 執行錯誤
127 輸出的結果127非0
預定義變量
表示形式如下
≌ $#:指令行中的位置參數的個數
≌ $*所有位置參數的内容
≌ $?:上一條指令執行後傳回的狀态,當傳回狀态值為0時表示執行正常,非0值表示執行異常或出錯
≌ $$:目前所在程序的程序号
≌ $!:背景運作的最後一個程序号
≌ $0:目前執行的程序/程式名
shell腳本的案例:
◎每周五17:30清理ftp伺服器的公共目錄
檢查/var/ftp/pub目錄,将其中所有子目錄及檔案的詳細清單、當時的時間資訊追加儲存到/var/log/pubdir.log檔案中。
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# chmod 755 a.sh
ls -lhR /var/ftp/pub >> /var/log/pubdir.log R也是一種列印清單的方式
date >> /var/log/pubdir.log
rm -rf /var/ftp/pub/*
[root@localhost ~]# crontab -l
30 17 * * 5 a.sh
注意;你所編寫的shell腳本要有可執行的權限755,a+x都行;
shell腳本的執行可以用. a.sh ./a.sh sh a.sh 等等來執行。
本文轉自青衫解衣 51CTO部落格,原文連結:http://blog.51cto.com/215687833/1181616