Linux:shell编程(学习笔记)
1.获取用户输入的值。
#!/bin/bash
function say_hello()
{
echo " Enter Your Name,Please. : "
read name
echo " Hello $name "
}
echo " Programme Starts Here… "
say_hello
echo " Programme Ends. "
2. if 语句。
#!/bin/bash
clear
echo " Input a file or directory name,please"
read path_name
if [ -d $path_name ]
then
echo " $path_name is a directory"
elif [ -f $path_name ]
then
echo " $path_name is a regular file"
if [ -r $path_name ]
then
echo " $path_name is a readable file"
fi
if [ -w $path_name ]
then
echo " $path_name is a writeable file"
fi
if [ -x $path_name ]
then
echo " $path_name is a executable file"
fi
else
echo " This script cannot get the file/directory($path_name) information!"
fi
3. if 语句。
#!/bin/bash
echo " Input a directory,please!"
read dir_name
if cd $dir_name > /dev/null 2>&1 ;then
echo " enter directory:$dir_name succeed"
else
echo " enter directory:$dir_name failded"
fi
4. case 语句。
#!/bin/bash
#an example script of case
clear
echo “Are you like Linux?”
read like_it
case $like_it in
y|Y|yes|Yes)echo “Linux is a good friend of us.”
;;
n|N|no|No)echo “Try it, and you will like it.”
;;
*)echo “you answer is: $like_it”
;;
Esac
5. for 语句。
#!/bin/bash
#an example script of for
clear
for os in Linux Windows Unix
do
echo “Operation System is:$os”
done
6. until 语句。
#!/bin/bash
#an example script of while
clear
loop=0
until [
l
o
o
p
−
e
q
10
]
d
o
l
e
t
l
o
o
p
=
loop -eq 10 ] do let loop=
loop−eq10]doletloop=loop+1
echo “currnet value of loop is:$loop”
done
7. while 语句。
#!/bin/bash
#an example script of while
clear
loop=0
while [
l
o
o
p
−
n
e
10
]
d
o
l
e
t
l
o
o
p
=
loop -ne 10 ] do let loop=
loop−ne10]doletloop=loop+1
echo “currnet value of loop is:$loop”
done
8. param 语句。
#!/bin/bash
#an example script of function
function demo_fun() #function definition starts
{
echo “Your command is:$0 $"
echo “Number of Parameters($#) is:$#”
echo “Script file name($0) is: $0”
echo "Parameters($) is:$*”
echo “Parameters($@) is:$@”
count=1
for param in $@
do
echo “Parameters($
c
o
u
n
t
)
i
s
:
count) is:
count)is:param”
let count=$count+1
done
} #function definition ends
clear
demo_fun $@
9. shift 语句。
#!/bin/bash
#an example script of function
function demo_fun() #function definition starts
{
echo “Your command is:$0 $"
echo “Number of Parameters($#) is:$#”
echo “Script file name($0) is: $0”
echo "Parameters($) is:$*”
echo “Parameters($@) is:$@”
count=1
while [ -n “$1” ]
do
echo "Parameters($$count) is:
1
"
l
e
t
c
o
u
n
t
=
1" let count=
1"letcount=count+1
shift
done
} #function definition ends
clear
demo_fun $@
10. return 语句。
#!/bin/bash
#an example script of return
function fun_return()
{
dir_name=$1
rtn_value=0
if ! cd $dir_name > /dev/null 2>&1 ;then
rtn_value=1
fi
return $rtn_value
}
clear
if fun_return $@ ;then
echo “function executes successfuly!”
else
echo “function executes failed!”
fi
11. return 语句。
#!/bin/bash
#an example script of return
function fun_return()
{
dir_name=$1
rtn_value=0
cd $dir_name > /dev/null 2>&1
return
}
clear
if fun_return $@ ;then
echo “function executes successfuly!”
else
echo “function executes failed!”
fi
12. bashrc 语句。
#!bashrc
#User specific aliases and functions
#Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
echo “Health is better than Wealth!”
export PS1="[\t]\u@\h/\w>"
13. mcp 语句。
#!/bin/bash
#an example script of backup files
LOG_START_TIME=date +"%Y%m%d%H%M%S"
BACKUP_DIR=~/backup
BACKUP_LOG="
B
A
C
K
U
P
D
I
R
/
BACKUP_DIR/
BACKUPDIR/{LOG_START_TIME}.log"
function write_log()
{
log_time=date +"%Y-%m-%d-%H-%M-%S"
backup_file_name=
2
e
r
r
m
s
g
=
"
2 err_msg="
2errmsg="log_time ERROR in backup file/directory(
b
a
c
k
u
p
f
i
l
e
n
a
m
e
)
"
s
u
c
m
s
g
=
"
backup_file_name)" suc_msg="
backupfilename)"sucmsg="log_time SUCCESS in backup file/directory($backup_file_name)"
if [ $1 -eq 0 ] ;then
echo $suc_msg
echo $suc_msg >> $BACKUP_LOG
else
echo $err_msg
echo $err_msg >> $BACKUP_LOG
fi
}
function backup_file()
{
cp -fr $1 $BACKUP_DIR > /dev/null 2>&1
write_log $? $1
}
function create_log_file()
{
if [ ! -e $BACKUP_DIR ] ;then
mkdir $BACKUP_DIR
fi
if [ -e $BACKUP_LOG ];then
rm -f $BACKUP_LOG
fi
touch $BACKUP_LOG
}
clear
echo “Backup Process Begins”
create_log_file
for file in $@
do
backup_file $file
done
echo “Backup Process Ends”