just for record some detail
like
sh -n ifshell.sh
sh -v ifshell.sh
sh -x ifshell.sh
``--get the command then execute ,output the result
expr--mean a expression
\*--translate
$*--all parameter,not include the shell name
$#--count the parameter number,not include the shell
$0--the shell name
$1--the first parameter
$2
-eq -ne -lt -le -gt -ge
! -a -o
= != str
for-- if use ; then
if no ;
then
[iyunv@u1 shell_learn]# sh ifshell.sh 5
5 number is postive
[iyunv@u1 shell_learn]# sh ifshell.sh -5
-5 number is negative
内容
#!/bin/bash
#comments
#$# get the input parameter , not include the command
if [ $# -ne 1 ]
#then must seperate if
then
echo "$0 : you must input a number"
exit 1
fi
#test like [ ]
if test $1 -gt 0
then
echo "$1 number is postive"
else
echo "$1 number is negative"
fi
if -else
[iyunv@u1 shell_learn]# sh ifelshell.sh
ifelshell.sh:you must a number
[iyunv@u1 shell_learn]# sh ifelshell.sh 5
5 is a positive
[iyunv@u1 shell_learn]# sh ifelshell.sh -5
-5 is a negative
[iyunv@u1 shell_learn]# sh ifelshell.sh 0
0 is equal 0
[iyunv@u1 shell_learn]# sh ifelshell.sh a
ifelshell.sh: line 10: [: a: integer expression expected
ifelshell.sh: line 13: [: a: integer expression expected
ifelshell.sh: line 16: [: a: integer expression expected
a is not a number
内容
#!/bin/bash
#comments
#test if ..elif else fi
if [ $# -ne 1 ]
then
echo "$0:you must a number"
exit 1
fi
#if elif else fi
if [ $1 -gt 0 ]
then
echo "$1 is a positive"
elif [ $1 -lt 0 ]
then
echo "$1 is a negative"
elif [ $1 -eq 0 ]
then
echo "$1 is equal 0"
else
echo "$1 is not a number"
fi
#!/bin/bash
#comment
#test while
if [ $# -ne 1 ]
then
echo "$0: must input a number"
exit 1
fi
i=0
while [ $i -le 10 ]
do
#``--mean calculate expr and output the result
echo "$1 * $i =`expr $1 \* $i`"
i=`expr $i + 1`
done
5、for
[iyunv@u1 shell_learn]# sh forshell.sh
weleome 1 times
weleome 2 times
weleome 3 times
11111
22222
33333
44444
55555
11111
22222
33333
44444
55555
内容
#!/bin/bash
#comments
#test for
for ii in 1 2 3
do
echo "weleome $ii times"
done
for(( i = 1; i <= 5; i++ ))
do
for(( j = 1; j <= 5; ++j ))
do
echo -n "$i"
done
# print a new line
echo ""
done
echo ""
# nested for
for(( i = 1; i <= 5; i++ ))
do
for(( j = 1; j <= 5; j++ ))
do
echo -n "$i"
done
#
echo ""
done
6、case
[iyunv@u1 shell_learn]# sh caseshell.sh
caseshell.sh: must input a command
[iyunv@u1 shell_learn]# sh caseshell.sh 1
1 is not a valid command
[iyunv@u1 shell_learn]# sh caseshell.sh delete
delete the db
[iyunv@u1 shell_learn]# sh caseshell.sh select
select the db
内容
#!/bin/bash
#comments
# test case
if [ $# -ne 1 ]
then
echo "$0: must input a command"
exit 1
fi
action=$1
case $action in
"update")
echo "update the db"
;;
"select")
echo "select the db"
;;
"delete")
echo "delete the db"
;;
*)
echo "$action is not a valid command"
;;
esac
7、function and args
[iyunv@u1 shell_learn]# sh funcshell.sh
shell name args:funcshell.sh
all function args:-f foo bar
all function agrs_num 3
the first arg : -f
the second arg : foo:
shell name args:funcshell.sh
all function args:foo bar
all function agrs_num 2
the first arg : foo
the second arg : bar:
内容
#!/bin/bash
#comments
# test function
function demo(){
echo "shell name args:$0"
echo "all function args:$*"
echo "all function agrs_num $#"
echo "the first arg : $1"
echo "the second arg : $2:"
shift
echo "shell name args:$0"
echo "all function args:$*"
echo "all function agrs_num $#"
echo "the first arg : $1"
echo "the second arg : $2:"
}
#call the function
demo -f foo bar
8、date
[iyunv@u1 shell_learn]# sh cmd_learn/date.sh
[iyunv@u1 shell_learn]# cat cmd_learn/date.log
2015-07-22 11:44:21
[iyunv@u1 shell_learn]# sh readshell.sh
1.unix(sun os)
2.linux(redhat)
select your os choice [1 or 2] ?2
you pick up linux
[iyunv@u1 shell_learn]# sh readshell.sh
1.unix(sun os)
2.linux(redhat)
select your os choice [1 or 2] ?4
what you donot like linux/unix
内容
#!/bin/sh
#comments
#define a variable
osch=0
#display prompt
echo "1.unix(sun os)"
echo "2.linux(redhat)"
echo -n "select your os choice [1 or 2] ?"
#wait user input
read osch
#if
if [ $osch -eq 1 ]
then
echo "you pick up unix"
else
#nested if
if [ $osch -eq 2 ]
then
echo "you pick up linux"
else
echo "what you donot like linux/unix"
fi
fi
12、rename file
[iyunv@u1 shell_learn]# ls
back_syncDeptFtp.sh forshell.sh ifshell.sh syncDeptFtp.sh
caseshell.sh funcshell.sh in_out.log whileshell.sh
cmd_learn getFtpFile.sh multisyncDeptFtp.sh who.out
data getSyncStatus.sh readme.txt
debugshell.sh helloshell.sh readshell.sh
find.log ifelshell.sh rename.sh
[iyunv@u1 shell_learn]# sh rename.sh
rename--renames a number of files using sed regular repressions
USAGE: rename 'regexp' 'relpacement' files
EXAMPLE:rename all *.HTM files in *.html
rename 'sh$' 'SH' *.sh
[iyunv@u1 shell_learn]# sh rename.sh sh$ SH *.sh
renaming back_syncDeptFtp.sh to back_syncDeptFtp.SH
renaming caseshell.sh to caseshell.SH
renaming debugshell.sh to debugshell.SH
renaming forshell.sh to forshell.SH
renaming funcshell.sh to funcshell.SH
renaming getFtpFile.sh to getFtpFile.SH
renaming getSyncStatus.sh to getSyncStatus.SH
renaming helloshell.sh to helloshell.SH
renaming ifelshell.sh to ifelshell.SH
renaming ifshell.sh to ifshell.SH
renaming multisyncDeptFtp.sh to multisyncDeptFtp.SH
renaming readshell.sh to readshell.SH
renaming rename.sh to rename.SH
renaming syncDeptFtp.sh to syncDeptFtp.SH
renaming whileshell.sh to whileshell.SH
[iyunv@u1 shell_learn]# ls
back_syncDeptFtp.SH forshell.SH ifshell.SH syncDeptFtp.SH
caseshell.SH funcshell.SH in_out.log whileshell.SH
cmd_learn getFtpFile.SH multisyncDeptFtp.SH who.out
data getSyncStatus.SH readme.txt
debugshell.SH helloshell.SH readshell.SH
find.log ifelshell.SH rename.SH
内容
#!/bin/bash
#comments
# test rename file
if [ $# -lt 3 ]
then
cat<<HELP
rename--renames a number of files using sed regular repressions
USAGE: rename 'regexp' 'relpacement' files
EXAMPLE:rename all *.HTM files in *.html
rename 'sh$' 'SH' *.sh
HELP
exit 0
fi
#
old=$1
new=$2
shift
shift
for file in $*
do
if [ -f $file ]; then
newfile=`echo $file | sed "s/${old}/${new}/g"`
if [ -f $newfile ] ;then
echo "error:$newfile exists already"
else
echo "renaming $file to $newfile"
mv $file $newfile
fi
fi
done