#----------------------------------------
#note:copy suffix with character "r" or "g"
# to "../test" directory
#!/bin/sh
for file in [rg]* ;do
mv $file ../test
done
#----------------------------------------
#--------------------------------------------------
echo "choose the city "
echo "1) WH"
echo "2) BJ"
echo "3) DN"
read input
case $input in
1) echo "you choose WuHan";;
2) echo "you choose BeiJin";;
3) echo "you choose DaNian";;
*) echo "input something number please!!!";;
esac
#--------------------------------------------
#name:check_file.sh
#note:check the arguments and file
#!/bin/sh
if [ $# -lt 1 ];then
echo "usage:`basename $0` filename"
echo
fi
if [ ! -f $2 ];then
echo "file is not exist"
fi
#---------------------------------
#name:check_root.sh
#note:check whether the user is root
ROOT_ID=0
if [ $UID -ne $ROOT_ID ] ; then
echo -e "you should change to root/n"
exit 1
fi
if [ -z $1 ] ;then
echo -e "miss arguments/n"
exit 1
fi
#-----------------------------------------------------------
#name:continue.sh
#note:print 1 through 20 except 4 and 14
for ((a=1;a<21;a++));do
if [ $a -eq 4 ] || [ $a -eq 14 ];then
continue
fi
echo "$a"
done
#-----------------------------------------------------------
#------------------------------------------------
#name:cut.sh
#note:print usrs in this computer
#!/bin/sh
FILE="/etc/passwd"
for user in $(cut -d: -f1 $FILE);do
echo "$user"
done
#-----------------------------------------------------
#name:nested_loop.sh
#note:for loop function nested for function
# output 25 lines
#!/bin/sh
LIMIT=6
for((a=1;a<LIMIT;a++));do
echo "the out loop is $a"
for((b=1;b<LIMIT;b++));do
echo "the inter loop is $b"
done
done
exit 0
#----------------------------------------------------
#------------------------------------------------
#name:find_exec.sh
#note:exec function used in find,look care "/;" at
# the end of the sentense
#!/bin/sh
#find /home/peter/shell -name "test.txt" -exec rm {} /;
find . -name "test.txt" -exec rm {} /;
#-------------------------------------
#name:for.sh
#using for function like C Language in shell
#!/bin/sh
LIMIT=10
for ((a=1;a<=LIMIT ; a++));do
echo "$a"
done
#-------------------------------------
#!/bin/bash
#This script print ip and network
file="/etc/sysconfig/network-scripts/ifcfg-eth0"
if [ -f $file ] ;then
IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
echo "$IP/$MASK"
exit 1
fi
#------------------------------------------------------------
#name:find_dir.sh
#list dir in $1 and print the result to out.txt
#!/bin/sh
ARG=1
OUTPUT="out.txt"
if [ $# -ne "$ARG" ]
#I can not understand here,I think the option should be "-eq"
then
DIR=`pwd`
else
DIR="$1"
fi
echo "----------directory list-------------" >>$OUTPUT
echo " this shell script are following"
for file in "$(find $DIR -type d)" ;do
echo "$file"|tee -a $OUTPUT
done
#-------------------------------------------------------------
#name:nested_continue.sh
#note:"continue 2" means skipped the second for loop function
for group in 1 2 3 4 5;do
echo "--group:$group--"
for ((inter=1;inter<=10;inter++));do
if [ $inter -gt 5 ];then
continue 2
fi
echo "inter:$inter"
done
done
exit 0
#-------------------------------------------------------------
#name:nested_continue.sh
#note:"continue 2" means skipped the second for loop function
for group in 1 2 3 4 5;do
echo "--group:$group--"
for ((inter=1;inter<=10;inter++));do
if [ $inter -gt 5 ];then
continue 2
fi
echo "inter:$inter"
done
done
exit 0
#read.sh
#read -t 10 delay 10s during input
#!/bin/sh
echo "input in 10s "
read -t 10 a
if [ -z $a ] ;then
echo "input again"
else
echo "input finish"
fi
#--------------------------------------------------------
#name:select.sh
#note:usage select function
#!/bin/sh
echo "choose your favorite fruit"
select fruit banana orange apple ;do
echo
echo "your choice is $fruit"
break
done
exit 0
#-----------------------------------
#name:shift.sh
#note:print the argument
while [ ! -e $1 ];do
echo -e "$1/n "
# echo
shift
done
#--------------------------------------------
#name:source.sh
#note: source function can insert other file
# into this file
#!/bin/sh
source test.sh
echo $a
#-----------------------------------
#name:while.sh
#note:input something and exit by type "end"
echo "input your sentense,type "end" to exit"
while [ "$var" != "end" ];do
#be careful ! $var must be quoted
read var
done
exit 0
#------------------------------------------------
#name:test_character.sh
#note:pick out the character you input
echo "please hit a character and type enter"
read a
case $a in
[a-z] ) echo "lower character";;
[A-Z] ) echo "upper character";;
[0-9] ) echo "digital";;
* ) echo "other symbol";;
esac
#----------------------------------------------------
#!/bin/sh
#name:test_equal.sh
#note:test two things whether are equal
clear
if [ $# -lt 2 ];then
echo "usage:$0 ARG1 ARG2"
exit 1
fi
test()
{
case $1 in
$2) echo "they are the same things ";;
* ) echo "they are different";;
esac
}
test $1 $2
exit 0
#name:test_file_exist.sh
#note:if the file in $FILE is not exist,then print warning,else print the filename
# and file size
#!/bin/sh
FILE="random.sh
read.sh
/bin/hi
"
for a in $FILE;do
if [ ! -e $a ];then
echo "warning :$a is no exist!!!"
continue
fi
echo
echo
ls -l $a|awk '{print "file_name:"$9 "file_size:"$5}'
done
#----------------------------------------------
#name:tr_d.sh
#note:delete colon from $1,and output a new file
#!/bin/sh
newname=$1.u
if [ -z $1 ] ;then
echo -e "usage:`basename $0` filename_to_convert/n"
exit 0
fi
tr -d ":"< $1 >$newname