測試IP地址
#/bin/bash
for i in 1 2 3 4 5 6 7 8 9do
echo "the number of $i computer is "
ping -c 5 192.168.0.$i #ping -c 5 是指ping 5次後停止。
done
/******************************************************************/
判斷test.sh 的大於指定大小 刪除 本目錄下其他文件
#!/bin/bash
a=2
while name="test.sh"
do
sleep 1
b=$(ls -l $name | awk '{print $5}' )
if test $b -ge $a
then
`rm awktest`
exit 0
fi
done
/******************************************************************/
11.從0.sh中讀取內容並打印
#/bin/bash
while read line
do
echo $line
done < 0.sh
/******************************************************************/
從1.sh中讀取每一行加一打印
#!/bin/bash
test -e 1.sh
while read line
do
a=$(($line+1))-------》1.sh中包含非數字默認爲0
#或#
a=`expr $line + 1` ----》1.sh中包含非數字報錯
echo $a
done<1.sh
/******************************************************************/
#!/bin/bash
p_num()
{
echo "$1" #$1:傳遞給函數的第一個參數
}
for n in $@ #$@ :傳遞給腳本的所有參數
do
p_num $n
done
/******************************************************************/
#!/bin/bash
while :
do
echo " please input file's name"
read a
if test -e ~/shell/$a # -e :檢測文件是否存在
then
echo "the file is existing please input new file name"
else
mkdir $a
echo "you are susscesful"
break
fi
done
/******************************************************************/
獲取本機IP地址
#/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
/******************************************************************/
獲取當前用戶
#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')
/******************************************************************/
case 練習
#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
1) echo "you enter 1"
;;
2) echo "you enter 2"
;;
3) echo "you enter 3"
;;
4) echo "you enter 4"
;;
5) echo "you enter 5"
;;
*) echo "error"
;;
esac
/******************************************************************/
內置命令練習
#/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date id `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
/******************************************************************/
檢測指定端口是否有服務佔用
#!/bin/bash
clear
n=1
echo "check XXX servers..."
while :
do
if test $n -gt 20
then
echo "XXX servers was falid"
break
fi
sleep 3
n=$(($n+1))
port=`netstat -antp | grep "0.0.0.0:80"`
if [ ${#port} -gt 3 ]; then
echo "XXX servers is started"
break
fi
done
/******************************************************************/