shell腳本。
殼,充當一個翻譯,讓計算機能夠認識的二進制程式,并将結果翻譯給我們。
加在核心上,可以跟核心打交道的殼。
可以通過
/etc/shells
來檢視。
[[email protected] ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
可以增加shell,
yum install zsh
[[email protected] ~]# zsh
[[email protected]]~# cd /etc/sysconfig
[[email protected]]/etc/sysconfig#
zsh可以顯示絕對路徑。
最常用的shell是bash。
編寫一個shell腳本。
shell是以.sh結尾的檔案。(linux不以字尾名區分檔案,為了友善記憶,通常都以.sh結尾)
[[email protected] ~]# vim first.sh
#! /bin/bash
# This is my first shell-script
mkdir /root/shell
ifconfig
[[email protected] ~]# chmod +x first.sh
增加可執行的權限。
執行腳本
[[email protected] ~]# first.sh
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.70.77 netmask 255.255.255.0 broadcast 192.168.70.255
inet6 fe80::20c:29ff:fe6e:b72b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:6e:b7:2b txqueuelen 1000 (Ethernet)
RX packets 4917 bytes 408590 (399.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2825 bytes 817074 (797.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 124 bytes 12730 (12.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 124 bytes 12730 (12.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:8c:58:59 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
就會顯示出ip資訊以及建立一個shell檔案夾了。
執行腳本的不同方式。
絕對路徑。
相對路徑。
通過sh指令來執行。(不需要執行權限)
[[email protected] ~]# sh ./add.sh 100 200
100 + 200 = 300
[[email protected] ~]# which sh
/usr/bin/sh
[[email protected] ~]# ll /usr/bin/sh
lrwxrwxrwx. 1 root root 4 1月 2 15:54 /usr/bin/sh -> bash
通過bash來執行。(不需要執行權限)
[[email protected] ~]# which bash
/usr/bin/bash
[[email protected] ~]# bash ./add.sh 100 200
100 + 200 = 300
shell變量。
自定義變量,環境變量,位置變量,預定義變量。
一般使用echo來輸出變量。
[[email protected] ~]# Linux=7.2
[[email protected] ~]# linux=7.20
[[email protected] ~]# echo $Linux
7.2
[[email protected] ~]# echo $linux
7.20
區分大小寫。
當變量與字元容易混淆的時候可以使用大括号括起來。
[[email protected] ~]# echo ${Linux} system
7.2 system
read 接收參數。
[[email protected] ~]# read dell hp
1 2
[[email protected] ~]# echo $dell $hp
1 2
[[email protected] ~]# read -p "輸入您的密碼:" passwd
輸入您的密碼:123456
[[email protected] ~]# echo $passwd
123456
shell中的數值運算,通過内部指令expr指令來運算。
[[email protected] ~]# A=10
[[email protected] ~]# B=20
[[email protected] ~]# exp
expand export exportfs expr
[[email protected] ~]# expr $A + $B
30
[[email protected] ~]# expr $A - $B
-10
[[email protected] ~]# expr $A * $B
expr: 文法錯誤
[[email protected] ~]# expr $A \* $B
200
[[email protected] ~]# expr $A / $B
0
[[email protected] ~]# expr $A % $B
10
儲存計算的結果。
[[email protected] ~]# abc=$(expr $A + $B)
[[email protected] ~]# echo $abc
30
特殊的shell變量。
環境變量,由系統本身運作需要提前建立好的一類變量。
[[email protected] ~]# env
XDG_SESSION_ID=19
HOSTNAME=local.rhel7.2-77.com
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.70.33 62585 22
SSH_TTY=/dev/pts/1
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:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=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:*.webm=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:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root:/root/bin
MAIL=/var/spool/mail/root
PWD=/root
LANG=zh_CN.UTF-8
HISTCONTROL=ignoredups
HOME=/root
SHLVL=3
LOGNAME=root
SSH_CONNECTION=192.168.70.33 62585 192.168.70.77 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
OLDPWD=/etc/sysconfig
環境變量存放位置,
/etc/profile