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