關於shell腳本:
一、用Shell 編程,判斷一文件是否是存在,若是存在將其拷貝到 /dev 目錄下。shell
vi a.sh #!/bin/bash read -p "input your filename:" A if [ ! -f $A ];then cp -f $A /dev fi
二、shell腳本,判斷一個文件是否存在,不存在就建立,存在就顯示其路徑編程
vi shell.sh #!/bin/bash read -p "請輸入文件名:" file if [ ! -f $file ];then echo "$file的路徑:$(find / -name $file)" else mkdir $file echo "$file 已存在" fi
三、寫出一個shell腳本,根據你輸入的內容,顯示不一樣的結果bash
#!/bin/bash read -p "請輸入你的內容:" N case $N in [0-9]*) echo "數字" ;; [a-z]|[A-Z]) echo "小寫字母" ;; *) echo "標點符號、特殊符號等" esac
四、寫一個shell腳本,當輸入foo,輸出bar,輸入bar,輸出foo網絡
vi shell.sh #!/bin/bash read -p "請輸入【foo|bar】:" A case $A in foo) echo "bar" ;; bar) echo "foo" ;; *) echo "請輸入【foo|bar】其中一個" esac
五、寫出一個shell腳本,能夠測試主機是否存活的ide
#!/bin/bash 單臺主機: ping -c3 -w1 192.168.80.100 if [ $? -eq 0 ];then echo "該主機up" else echo "該主機down" fi 多臺主機: P=192.168.80. for ip in {1..255} do ping -c3 -w1 $P$ip if [ $? -eq 0 ];then echo "該$P$ip主機up" else echo "該$P$ip主機down" fi done
六、寫一個shell腳本,輸出/opt下全部文件函數
vi shell.sh 第一種: #!/bin/bash find /opt -type f 第二種: #!/bin/bash for A in $(ls /opt) do if [ ! -f $A ];then echo $A fi done
七、編寫shell程序,實現自動刪除50個帳號的功能。帳號名爲stud1至stud50。測試
vi shell.sh #!/bin/bash i=1 while [ $i -le 50 ] do userdel -r stud$i let i++ done
八、用shell腳本建立一個組class、一組用戶,用戶名爲stdX X從1-30,並歸屬class組code
vi shell.sh 第一種: #!/bin/bash groupadd class for X in std{1..30} do useradd -G class $X done 第二種: #!/bin/bash X=1 while [ $X -le 30 ] do useradd -G class std$X let X++ done
九、寫一個腳本,實現判斷192.168.80.0/24網絡裏,當前在線的IP有哪些,能ping通則認爲在線ip
vi shell.sh #!/bin/bash for ip in 192.168.80.{1..254} do ping -c3 -w0.1 $ip &> /dev/null if [ $? -eq 0 ];then echo "$ip 存活" else echo "$ip 不存活" fi done
十、寫一個shell腳本,能夠獲得任意兩個數字相加的和input
vi shell.sh #!/bin/bash sum = $(($1 + $2)) echo "$1 + $2 = $sum" shell.sh 1 2
十一、定義一個shell函數運行乘法運算
#!/bin/bash sum(){ SUM=$(($1*$2)) echo "$1*$2=$SUM" } sum $1 $2
十二、寫出一個shell腳本,判斷兩個整數的大小,若是第一個整數大於第二個整數那就輸出第一個整數,不然輸出第二個整數
#!/bin/bash if [ $1 -gt $2 ];then echo "$1大" else echo "$2大" fi
1三、shell腳本,九九乘法表
vi shell.sh #!/bin/bash a=1 while [ $a -le 9 ] do b=1 while [ $b -le $a ] do echo -n -e "$a * $b = $(($a * $b))\t" let b++ done echo "" let a++ done