關(guān)于shell腳本:
1、用Shell 編程,判斷一文件是不是存在,如果存在將其拷貝到 /dev 目錄下。
vi a.sh#!/bin/bashread -p "input your filename:" Aif [ ! -f $A ];then cp -f $A /devfi
2、shell腳本,判斷一個文件是否存在,不存在就創(chuàng)建,存在就顯示其路徑
vi shell.sh#!/bin/bashread -p "請輸入文件名:" fileif [ ! -f $file ];then echo "$file的路徑:$(find / -name $file)"else mkdir $file echo "$file 已存在"fi
3、寫出一個shell腳本,根據(jù)你輸入的內(nèi)容,顯示不同的結(jié)果
#!/bin/bashread -p "請輸入你的內(nèi)容:" Ncase $N in [0-9]*) echo "數(shù)字" ;; [a-z]|[A-Z]) echo "小寫字母" ;; *) echo "標(biāo)點(diǎn)符號、特殊符號等"esac
4、寫一個shell腳本,當(dāng)輸入foo,輸出bar,輸入bar,輸出foo
vi shell.sh#!/bin/bashread -p "請輸入【foo|bar】:" Acase $A in foo) echo "bar" ;; bar) echo "foo" ;; *) echo "請輸入【foo|bar】其中一個"esac
5、寫出一個shell腳本,可以測試主機(jī)是否存活的
#!/bin/bash單臺主機(jī):ping -c3 -w1 192.168.80.100if [ $? -eq 0 ];then echo "該主機(jī)up"else echo "該主機(jī)down"fi多臺主機(jī):P=192.168.80.for ip in {1..255}do ping -c3 -w1 $P$ip if [ $? -eq 0 ];then echo "該$P$ip主機(jī)up" else echo "該$P$ip主機(jī)down" fidone
6、寫一個shell腳本,輸出/opt下所有文件
vi shell.sh第一種:#!/bin/bashfind /opt -type f 第二種:#!/bin/bashfor A in $(ls /opt)do if [ ! -f $A ];then echo $A fidone
7、編寫shell程序,實(shí)現(xiàn)自動刪除50個賬號的功能。賬號名為stud1至stud50。
vi shell.sh#!/bin/bashi=1while [ $i -le 50 ]douserdel -r stud$ilet i done
8、用shell腳本創(chuàng)建一個組class、一組用戶,用戶名為stdX X從1-30,并歸屬class組
vi shell.sh第一種:#!/bin/bashgroupadd classfor X in std{1..30}do useradd -G class $Xdone第二種:#!/bin/bashX=1while [ $X -le 30 ]do useradd -G class std$X let X done
9、寫一個腳本,實(shí)現(xiàn)判斷192.168.80.0/24網(wǎng)絡(luò)里,當(dāng)前在線的IP有哪些,能ping通則認(rèn)為在線
vi shell.sh#!/bin/bashfor ip in 192.168.80.{1..254}do ping -c3 -w0.1 $ip &> /dev/null if [ $? -eq 0 ];then echo "$ip 存活" else echo "$ip 不存活" fidone
10、寫一個shell腳本,可以得到任意兩個數(shù)字相加的和
vi shell.sh#!/bin/bashsum = $(($1 $2))echo "$1 $2 = $sum"shell.sh 1 2
11、定義一個shell函數(shù)運(yùn)行乘法運(yùn)算
#!/bin/bashsum(){ SUM=$(($1*$2)) echo "$1*$2=$SUM"}sum $1 $2
12、寫出一個shell腳本,判斷兩個整數(shù)的大小,如果第一個整數(shù)大于第二個整數(shù)那就輸出第一個整數(shù),否則輸出第二個整數(shù)
#!/bin/bashif [ $1 -gt $2 ];then echo "$1大" else echo "$2大"fi
13、shell腳本,九九乘法表
vi shell.sh#!/bin/basha=1while [ $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