幸运儿不是我,因为我选择的路很难走.如果够出色也不能出头,至少要做到没第二个我.
分类: LINUX
2012-10-30 08:17:39
1.在linux里面是不在乎后缀名的,但是建议写上后缀名,如test.sh,这样一眼便看出这是shell程序。
2.如果不能运行,一般要执行chmod +x filename 使文件可执行
3.执行格式一般为./test.sh,为了安全起见。
4.写shell脚本时最好要建立良好的习惯。 在每个 script 的档头处记录好∶(练习的时候免了吧)
script 的功能;
script 的版本资讯;
script 的作者与联络方式;
script 的版权宣告方式;
script 的 History (历史纪录);
script 内较特殊的指令,使用绝对路径的方式来下达;
script 运作时需要的环境变数预先宣告与设定。
来自: 鸟哥的私房菜
注:鸟哥的shell用的是bash,不过建议写成 #!/bin/sh这样就可以使用系统默认版本的shell,而不一定就是用bash。
在获取命令的运行结果中,鸟哥用的是`(不是单引号'),建议用$(),更好一些。
# 请建立一支 script ,当你执行该 script 的时候,该 script 可以显示∶ 1. 你目前的身份 (用 whoami ) 2. 你目前所在的目录 (用 pwd)
view plaincopy to clipboardprint?
#!/bin/bash
echo -e "Your name is ==> $(whoami)"
echo -e "The current directory is ==> `pwd`"
#!/bin/bash
echo -e "Your name is ==> $(whoami)"
echo -e "The current directory is ==> `pwd`"
# 请自行建立一支程式,该程式可以用来计算『您还有几天可以过生日』啊??
view plaincopy to clipboardprint?
#!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
now=`date +%m%d`
if [ "$bir" == "$now" ]; then
echo "Happy Birthday to you!!!"
elif [ "$bir" -gt "$now" ]; then
year=`date +%Y`
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
echo "Your birthday will be $total_d later"
else
year=$((`date +%Y`+1))
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
echo "Your birthday will be $total_d later"
fi
#!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
now=`date +%m%d`
if [ "$bir" == "$now" ]; then
echo "Happy Birthday to you!!!"
elif [ "$bir" -gt "$now" ]; then
year=`date +%Y`
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
echo "Your birthday will be $total_d later"
else
year=$((`date +%Y`+1))
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
echo "Your birthday will be $total_d later"
fi
# 让使用者输入一个数字,程式可以由 1+2+3... 一直累加到使用者输入的数字为止。
view plaincopy to clipboardprint?
#!/bin/bash
read -p "Please input an integer number: " number
i=0
s=0
while [ "$i" != "$number" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "the result of '1+2+3+...$number' is ==> $s"
#!/bin/bash
read -p "Please input an integer number: " number
i=0
s=0
while [ "$i" != "$number" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "the result of '1+2+3+...$number' is ==> $s"
# 撰写一支程式,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若不存在,则建立一个档案,使用 touch 来建立,建立完成后离开; 3.) 如果存在的话,判断该名称是否为档案,若为档案则将之删除后建立一个档案,档名为 logical ,之后离开; 4.) 如果存在的话,而且该名称为目录,则移除此目录!
view plaincopy to clipboardprint?
#!/bin/bash
if [ ! -e logical ]; then
touch logical
echo "Just make a file logical"
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo "remove file ==> logical"
echo "and make directory logical"
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo "remove directory ==> logical"
exit 1
else
echo "Does here have anything?"
fi
#!/bin/bash
if [ ! -e logical ]; then
touch logical
echo "Just make a file logical"
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo "remove file ==> logical"
echo "and make directory logical"
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo "remove directory ==> logical"
exit 1
else
echo "Does here have anything?"
fi
# 我们知道 /etc/passwd 里面以 : 来分隔,第一栏为帐号名称。请写一苹程式,可以将 /etc/passwd 的第一栏取出,而且每一栏都以一行字串『The 1 account is "root" 』来显示,那个 1 表示行数。
view plaincopy to clipboardprint?
#!/bin/bash
accounts=`cat /etc/passwd | cut -d':' -f1`
for account in $accounts
do
declare -i i=$i+1
echo "The $i account is \"$account\" "
done
#!/bin/bash
accounts=`cat /etc/passwd | cut -d':' -f1`
for account in $accounts
do
declare -i i=$i+1
echo "The $i account is \"$account\" "
done
来自:http://sucre.javaeye.com/blog/625918
1. 写一个脚本,利用循环计算10的阶乘
view plaincopy to clipboardprint?
#!/bin/sh
factorial=1
for a in `seq 1 10`
do
factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"
#!/bin/sh
factorial=1
for a in `seq 1 10`
do
factorial=`expr $factorial \* $a`
done
echo "10! = $factorial"
注:上面有一行,for a in `seq 1 10`,其中seq 1 10 , 即列出现1到10之间所有的数字,这一行也可改为:for a in "1 2 3 4 5 6 7 8 9 10"
2. 写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然
后打印出该数值,
然后再次要求用户输入数值。直到用户输入
"end"停止。
view plaincopy to clipboardprint?
#!/bin/sh
unset var
while [ "$var" != "end" ]
do
echo -n "please input a number: "
read var
if [ "$var" = "end" ]
then
break
fi
echo "var is $var"
done
#!/bin/sh
unset var
while [ "$var" != "end" ]
do
echo -n "please input a number: "
read var
if [ "$var" = "end" ]
then
break
fi
echo "var is $var"
done
3. 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和
view plaincopy to clipboardprint?
#!/bin/sh
sum=0
for a in `seq 1 100`
do
if [ `expr $a % 3` -ne 0 ]
then
continue
fi
echo $a
sum=`expr $sum + $a`
done
echo "sum = $sum"
#!/bin/sh
sum=0
for a in `seq 1 100`
do
if [ `expr $a % 3` -ne 0 ]
then
continue
fi
echo $a
sum=`expr $sum + $a`
done
echo "sum = $sum"
4.一个函数,利用shift计算所有参数乘积,假设参数均为整数( 特殊变量$# 表示包含参数的个数)
view plaincopy to clipboardprint?
#! /bin/sh
result=1
while [ $# -gt 0 ]
do
result=`expr $result \* $1`
shift
done
echo $resul
#! /bin/sh
result=1
while [ $# -gt 0 ]
do
result=`expr $result \* $1`
shift
done
echo $resul
5.写一个脚本,可以根据参数文件名,以正确的参数调用tar来解压缩tar.gz或tar.bz2文件。
view plaincopy to clipboardprint?
#!/bin/sh
case ${1##*.tar.} in
bz2)
tar jxvf $1
;;
gz)
tar zxvf $1
;;
*)
echo "wrong file type"
esac
#!/bin/sh
case ${1##*.tar.} in
bz2)
tar jxvf $1
;;
gz)
tar zxvf $1
;;
*)
echo "wrong file type"
esac
6.写一个脚本以方便用户查询rpm的相关信息。这个脚本首先提示用户选择查询依据,比如
文件名,包名,全部等。然后提示用户选择查询信息,比如包名,包里所包含的所有文件,
包的信息等。然后询问是否继续查询,是则循环刚才的过
程,否则退出。
view plaincopy to clipboardprint?
#!/bin/sh
RPM=/bin/rpm
option="-q"
while true
do
echo "what to query?"
select var in "All" "file" "package name"
do
case $var in
All)
option=$option"a"
break
;;
file)
echo -n "please input file name: "
option=$option"f"
read argument
break
;;
package\ name)
echo -n "please input package name: "
read argument
break
;;
*)
echo "please choose between 1-3"
;;
esac
done
echo "what do you want to know?"
select var in "location" "info" "package name"
do
case $var in
location)
option=$option"l"
break
;;
info)
option=$option"i"
break
;;
package\ name)
break
;;
*)
echo "please choose between 1-3"
;;
esac
done
${RPM} $option $argument
echo "continue? [yes/no]"
read answer
if [ answer = "no" ]
then
break
fi
done
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/plo154100/archive/2010/08/21/5828019.aspx