linux 下的shell脚本
SHELL
chsh -l? 查看shell
切換
1.臨時?? 直接執行?? /bin/sh
2.永久?? chsh???
bash? 特點、
1.交互式
2.命令的歷史功能 history !+指令編號
3.命令的補齊? TAB
4.通配符? *任意字符?? ?匹配一個字符 【!1-9】
5.前臺后臺切換? fg? bg?
ps -aux 查看進程
jobs 查看正在進行或暫停任務
bg 1 將一個任務切換到后臺運行
fg 1 將任務切換到前臺運行
6.特殊字符?? 若引用 “”? 強引用?? ''???? 命令取代 ``
7.輸入輸出重定向
? 0 標準輸入
? 1? 標準輸出
? 2? 變準出錯
? &> 所有的輸出? = 2>&1
find / -size +10M 2>1.txt 將出錯的輸入到1.txt中
mail root -s ok1 0< 1.txt 正確的輸入到1.txt并以郵件形式發送出去
8.命令別名
?
環境變量
set
1.本地變量?
2.環境變量
3. 特殊變量
a? 位置變量?? $1 $2? $0顯示shell名
b? 狀態變量
環境文件
系統 /etc/profile?? /etc/bashrc
個人? ~/.bash_profile?? ~/bashrc
umask? 權限掩碼
022
最高權限-umask=實際權限
文件文件最高權限 666
目錄最高權限 777
umask 022 修改權限為022
vim /etc/bashrc 編輯權限設置
alias=`find / -size +10m` 執行alias這自動收索大于10M的文件
alias find10=`find / -size +10M` 執行find10這自動收索大于10M的文件
算數運算
let
A=let 1+2 對1+2進行求和
?
$【】
A=$[1+2] 對1+2進行求和
$(())
A=$((1+3)) 對1+3進行求和
bc
echo "scale=2;5/2" |bc 求5/2的值
判斷
test? expression
test 1 -gt 2
echo $? 判斷1是否大于2
【 表達式 】
【 1 -gt 2】
? echo $?? 判斷1是否大于2
數字比較
大于?? -gt
大于等于 -ge
小于?? -lt
小于等于? -le
等于? -eq?
不等于? ! -eq
字符串比較
大于 >? (一對【】帶轉義符??? 兩隊【】? 不加轉義符)
A=hello ,b=hellol
[ $A \> $B ] [[ $A > $B ]]? 判斷A是否大于B
小于 <??? (一對【】帶轉義符??? 兩隊【】? 不加轉義符)
等于 =???? ==? (等號兩邊有空格)
[ $A = $B ]
對象
-f? 文件
[ -f f1 ]
echo $??? 判斷f1是否為一個文件
-e 存在
-d? 目錄
-L 連接
-r? 讀取
-x? 執行
與? -a (兩個判斷式) 或? -o?
[ -e f1 -a -r f1 ] 判斷f1是存在與可讀取
短路操作符
&&? 與(兩個語句)?? ||??
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,并將收索結果放置/dev/null中
用腳本實現
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用紅色表示
控制語句
選擇
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
判斷一個文件是否存在,并判斷它的類型
#!/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語句
case? 變量? in
變量值1)
.......;;
變量值2)
......;;
變量值3)
....;;
*)
....;;
esac
shell 腳本實現判斷文件類型
#!/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
腳本運行的時候 ,提示輸入帳號,判斷該帳號是否存在 ,如果存在 顯示該用戶的shell? 以及改用的家目錄,以及該用戶密碼是否設置
#!/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
?
?
循環
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內偶數求和
#!/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內奇數求和
#!/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 <文件名
查看一個文件內容在每個目錄前顯示行號
#!/bin/bash
let I=1
while read LINE;do
echo? -e "$I:\t" $LINE
let? I=$I+1
done </etc/passwd
顯一個文件內容并判斷文件類型
#!/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
?
作業? 挑選? 普通帳號? ,沒賬號的前面加上序號
#!/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
?
判斷一個網絡某個用戶是否存在
#!/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 查看當前用戶最大uid
不用useradd創建一個用戶并以腳本的方式實現
#!/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
轉載于:https://blog.51cto.com/4459021/788864
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的linux 下的shell脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: J2EE 中的安全第一部分 - J2EE
- 下一篇: 在Linux机器上配置NUD