linux 下的shell脚本
SHELL
chsh -l? 查看shell
切換
1.臨時(shí)?? 直接執(zhí)行?? /bin/sh
2.永久?? chsh???
bash? 特點(diǎn)、
1.交互式
2.命令的歷史功能 history !+指令編號(hào)
3.命令的補(bǔ)齊? TAB
4.通配符? *任意字符?? ?匹配一個(gè)字符 【!1-9】
5.前臺(tái)后臺(tái)切換? fg? bg?
ps -aux 查看進(jìn)程
jobs 查看正在進(jìn)行或暫停任務(wù)
bg 1 將一個(gè)任務(wù)切換到后臺(tái)運(yùn)行
fg 1 將任務(wù)切換到前臺(tái)運(yùn)行
6.特殊字符?? 若引用 “”? 強(qiáng)引用?? ''???? 命令取代 ``
7.輸入輸出重定向
? 0 標(biāo)準(zhǔn)輸入
? 1? 標(biāo)準(zhǔn)輸出
? 2? 變準(zhǔn)出錯(cuò)
? &> 所有的輸出? = 2>&1
find / -size +10M 2>1.txt 將出錯(cuò)的輸入到1.txt中
mail root -s ok1 0< 1.txt 正確的輸入到1.txt并以郵件形式發(fā)送出去
8.命令別名
?
環(huán)境變量
set
1.本地變量?
2.環(huán)境變量
3. 特殊變量
a? 位置變量?? $1 $2? $0顯示shell名
b? 狀態(tài)變量
環(huán)境文件
系統(tǒng) /etc/profile?? /etc/bashrc
個(gè)人? ~/.bash_profile?? ~/bashrc
umask? 權(quán)限掩碼
022
最高權(quán)限-umask=實(shí)際權(quán)限
文件文件最高權(quán)限 666
目錄最高權(quán)限 777
umask 022 修改權(quán)限為022
vim /etc/bashrc 編輯權(quán)限設(shè)置
alias=`find / -size +10m` 執(zhí)行alias這自動(dòng)收索大于10M的文件
alias find10=`find / -size +10M` 執(zhí)行find10這自動(dòng)收索大于10M的文件
算數(shù)運(yùn)算
let
A=let 1+2 對(duì)1+2進(jìn)行求和
?
$【】
A=$[1+2] 對(duì)1+2進(jìn)行求和
$(())
A=$((1+3)) 對(duì)1+3進(jìn)行求和
bc
echo "scale=2;5/2" |bc 求5/2的值
判斷
test? expression
test 1 -gt 2
echo $? 判斷1是否大于2
【 表達(dá)式 】
【 1 -gt 2】
? echo $?? 判斷1是否大于2
數(shù)字比較
大于?? -gt
大于等于 -ge
小于?? -lt
小于等于? -le
等于? -eq?
不等于? ! -eq
字符串比較
大于 >? (一對(duì)【】帶轉(zhuǎn)義符??? 兩隊(duì)【】? 不加轉(zhuǎn)義符)
A=hello ,b=hellol
[ $A \> $B ] [[ $A > $B ]]? 判斷A是否大于B
小于 <??? (一對(duì)【】帶轉(zhuǎn)義符??? 兩隊(duì)【】? 不加轉(zhuǎn)義符)
等于 =???? ==? (等號(hào)兩邊有空格)
[ $A = $B ]
對(duì)象
-f? 文件
[ -f f1 ]
echo $??? 判斷f1是否為一個(gè)文件
-e 存在
-d? 目錄
-L 連接
-r? 讀取
-x? 執(zhí)行
與? -a (兩個(gè)判斷式) 或? -o?
[ -e f1 -a -r f1 ] 判斷f1是存在與可讀取
短路操作符
&&? 與(兩個(gè)語(yǔ)句)?? ||??
grep "^user1\>" /etc/passwd && echo "the account is exist " ||echo "the account is not exist"
在/etc/passwd下收索user1,如果存在顯示the account is exist否則顯示the account is not exist
grep "^user1\>" /etc/passwd? &>/dev/null && echo "the account is exist " ||echo "the account is not exist"
在/etc/passwd下收索user1,如果存在顯示the account is exist否則顯示the account is not exist,并將收索結(jié)果放置/dev/null中
用腳本實(shí)現(xiàn)
vim 1.sh
#!/bin/bash
read -p "please input one account:" ACCOUNT
grep "^$ACCOUNT\>" /etc/passwd &>/dev/null && echo "the $ACCOUNT is ok " ||echo "not ok "
echo -e "\t\033[31m123\033[0m\n"? 將123用紅色表示
控制語(yǔ)句
選擇
1。單選
?if? 【】;then
?.....
?fi
#!/bin/bash
read -p "please input one account:" ACCOUNT
if? grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then
?echo "the $ACCOUNT is ok "
fi
2.雙選
?if? [];then
? ..
?else
?..
fi
#!/bin/bash
read -p "please input one account:" ACCOUNT
if? grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then
?echo "the $ACCOUNT is ok "
else
echo “the $ACCOUNT is? not ok”
fi
3.多選
if? 【】;then
......
elif? 【】;then
......
elif? 【】;then
......
fi
判斷一個(gè)文件是否存在,并判斷它的類(lèi)型
#!/bin/bash
read -p "please input one file:" FILE
read -p "please input one dir:" DIR
if [ -e $DIR/$FILE ];then
if [ -f $DIR/$FILE ];then
echo "the $DIR/$FILE is a file"
elif [ -d $DIR/$FILE ];then
echo "the $DIR/$FILE is a dir"
elif [ -L $DIR/$FILE ];then
echo "the $DIR/$FILE is a link"
fi
else
echo "the $DIR/$FILE is not ok"
fi
3.case語(yǔ)句
case? 變量? in
變量值1)
.......;;
變量值2)
......;;
變量值3)
....;;
*)
....;;
esac
shell 腳本實(shí)現(xiàn)判斷文件類(lèi)型
#!/bin/bash
read -p "please input one file:" FILE
read -p "please input one dir:" DIR
if [ -e $DIR/$FILE ];then
STRING=`/bin/ls -l -d $DIR/$FILE`
FIRSTCHAR=`echo $(STRING:0:1)`
case $FIRSTCHAR in
-)
echo "the $DIR/$FILE is file";;
d)
echo "the $DIR/$FILE is dir";;
l)
echo "the $DIR/$FILE is link";;
b)
echo "the $DIR/$FILE is block device";;
c)
echo "the $DIR/$FILE is char device";;
*)
exit
else
echo "the $DIR?$FILE is not exist"
exit
esac
fi
腳本運(yùn)行的時(shí)候 ,提示輸入帳號(hào),判斷該帳號(hào)是否存在 ,如果存在 顯示該用戶(hù)的shell? 以及改用的家目錄,以及該用戶(hù)密碼是否設(shè)置
#!/bin/bash
read -p "please input one account:" ACCOUNT
if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then
HOMEDIR1=`grep "^$ACCOUNT\>" /etc/passwd |cut -d: -f6`
shell1=`grep "^$ACCOUNT\>" /etc/passwd |cut -d: -f7`
echo "the user $ACCOUNT is exist"
echo "the user home is $HOMEDIR1"
echo "the user shell is $shell1"
/usr/bin/passwd -S $ACCOUNT &>/tmp/state
if grep -i "lock" /tmp/state &>/dev/null;then
echo "passwd is lock"
elif grep -i "empty" /tmp/state &>/dev/null;then
echo "passwd is empty"
elif grep -i "md5" /tmp/state &>/dev/null;then
echo "passwd is encry"
fi
else
echo "the user $ACCOUNT is not exist"
fi
?
?
循環(huán)
for? 變量?? in? 變量值;do
........
done
1到100求和
#!/bin/bash
I=1
SUM=0
for I in {1..100};do
let SUM=$SUM+$I
done
echo $SUM
1到100內(nèi)偶數(shù)求和
#!/bin/bash
I=1
SUM=0
for I in {1..100};do
if [$[ $I%2 ] = 0 ];then
let SUM=$SUM+$I
done
echo $SUM
1到100內(nèi)奇數(shù)求和
#!/bin/bash
I=1
SUM=0
for I in {1..100};do
if [$[ $I%2 ] != 0 ];then
let SUM=$SUM+$I
done
echo $SUM
?
while? [條件];do
done
#!/bin/bash
I=1
SUM=0
while [ $I -le 100 ];do
let SUM=$SUM+$I
let I=$I+1
done
echo $SUM
?
while? read? LINE;do
done <文件名
查看一個(gè)文件內(nèi)容在每個(gè)目錄前顯示行號(hào)
#!/bin/bash
let I=1
while read LINE;do
echo? -e "$I:\t" $LINE
let? I=$I+1
done </etc/passwd
顯一個(gè)文件內(nèi)容并判斷文件類(lèi)型
#!/bin/bash
read -p "please input one directory:" DIR
/bin/ls $DIR &>/tmp/file
while read LINE;do
if [ -f $dir/$LINE ];then
echo -e "$LINE\t" "the $DIR/$LINE is a file"
elif [ -d $DIR/$LINE ];then
echo -e "$LINE\t"? "the $DIR/$LINE is a dir"
elif [ -L $DIR/$LINE ];then
echo -e "$LINE\t"? "the $DIR/$LINE is a link"
done </tmp/file
?
until [條件];do
done
#!/bin/bash
#!/bin/bash
I=1
SUM=0
until [ $I -ge 100 ];do
let SUM=$SUM+$I
let I=$I+1
done
echo $SUM
?
作業(yè)? 挑選? 普通帳號(hào)? ,沒(méi)賬號(hào)的前面加上序號(hào)
#!/bin/bash
I=1
while read LINE;do
USERID=`echo $LINE |cut -d: -f3`
ACCOUNTNAME=`echo $LINE |cut -d: -f1`
if [ $USERID -ge 500 ];then
?echo -e? "\033[31m$I\t$ACCOUNTNAME\t$USERID\033[0m"
?let I=$I+1
?fi
done </etc/passwd
?
判斷一個(gè)網(wǎng)絡(luò)某個(gè)用戶(hù)是否存在
#!/bin/bash
for I in {1..254};do
if ping -c 1 -W 1 192.168.101.$I &>/dev/null;then
echo "the host 192.168.101.$I is exist"
else
echo "the host 192.168.101.$I is not exist"
fi
done
?
cut /etc/passwd -d: -f3 |sort -n |tail -2 |head -1 查看當(dāng)前用戶(hù)最大uid
不用useradd創(chuàng)建一個(gè)用戶(hù)并以腳本的方式實(shí)現(xiàn)
#!/bin/bash
read -p "please input one user:" ACCOUNT
if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then
exit
else
MAXID=`cut -d:-f3 /etc/passwd |sort -n |tail -2 |head -1`
echo "$ACCOUNT:x:$[ $MAXID+1 ]:$[ $MAXID+1]::/home/$ACCOUNT:/bin/bash" >>/etc/passwd
echo "$ACCOUNT::::::::">>/etc/shadow
echo “$ACCOUNT:x:$[ $MAXID+1 ]:" >>/etc/group
mkdir /home/$ACCOUNT
cp /etc/skel/.*? /home/$ACCOUNT &>/dev/null
chown -R $ACCOUNT.$ACCOUNT /home/$ACCOUNT
touch /var/spool/mail/$ACCOUNT
chmod 660 /var/spool/mail/$ACCOUNT
chown $ACCOUNT.mail /var/spool/mail/$ACCOUNT
echo "123" |passwd --stdin $ACCOUNT
fi
轉(zhuǎn)載于:https://blog.51cto.com/4459021/788864
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的linux 下的shell脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: J2EE 中的安全第一部分 - J2EE
- 下一篇: linux 其他常用命令