1.输入两个数,比较着两个数的大小
eg:#!/bin/bash
A=$1
B=$2
let A+=1
let B+=1
echo "The sum is $(($A+$B))"
2.用sed调用其命令脚本/root/txt及其方法
eg:#!/bin/bash
s/file/FILE/g
s/a/A/g
调用方法:sed -f /root/txt /etc/passswd
3.if语句的初次用法
eg:#!/bin/bash
if cut -d: -f1 /etc/passwd | grep "^$1$" &> /dev/null ; then
echo "$1 is there"
else
echo "$1 is not there"
fi
4.判断输入文件的类型
eg:
#!/bin/bash
if [ -e $1 ]; then
if [ -f $1 ]; then
echo "$1 is a common file."
elif [ -d $1 ];then
echo "$1 is a directory."
else
echo "$1 is unkown."
fi
else
echo "You fool,give me a correct file."
exit 7
fi
5.用for 语句从1加到100的小脚本
eg:
#!/bin/bash
let SUM=0
for I in $(seq 1 100);do
let SUM+=$I
done
echo "The sum is:$SUM."
6.有关ping的小脚本
eg:
#!/bin/bash
for I in {1..10}; do
if ping -c1 -w2 192.168.0.$I &> /dev/null; then
echo "$I is online."
else
echo "$I is offline."
fi
done
7.查看/var/目录内的内容
eg:
#!/bin/bash
cd /var
for I in ./*;do
file $I
done
8.输入两个数,让其相加
eg:
#!/bin/bash
read -p "Please input an integer:" A
read -p "Please input an integer:" B
echo "The sum is $[$A+$B]."
9.要求用户输入一个文件名,判断如果此文件时普通文件,显示共有多少行。
eg:
#!/bin/bash
read -p "Please assign a file:" FILE
let COUNT=0
while read LINE; do
let COUNT++
done < $FILE
echo $COUNT
这个文件没有判断文件的类型,(缺陷)
10.创建newscript脚本
eg:
#!/bin/bash
if ! grep "^#!" $1;then
cat >> $1 << EOF
#!/bin/bash
#Author:
#Date & Time:`date +"%F %T"`
#Description:
EOF
fi
vim +5 $1
11.写一个newscript小脚本,来创建小脚本文件
eg:
#!/bin/bash
if ! grep "^#!" $1 &> /dev/null ;then
cat >> $1 << EOF
#!/bin/bash
#Author:
#Date & Time:`date +"%F %T"`
#Description:
EOF
fi
vim +5 $1
chmod u+x $1
将文件移到/bin中,mv newscript /bin
12.比较三个数的大小
eg:
#!/bin/bash
read -p "A number:" A
read -p "B number:" B
read -p "C number:" C
if [ $A -ge $B ];then
MAX=$A
else
MAX=$B
fi
if [ $MAX -le $C ];then
MAX=$C
fi
echo "The max number is:$MAX."
比较三个数大小的另一种方法:
eg:
#!/bin/bash
#Author:
#Date & Time:2010-12-24 16:40:54
#Description:
read A
read B
read C
if [ $A -ge $B ];then
if [ $A -ge $C ];then
echo "Max is $A."
else
echo "Max is $C."
fi
else
if [ $B -ge $C ];then
echo "Max is $B."
else
echo "Max is $C."
fi
fi