#!/bin/bash
#This script is show now time and who login.
date
who
# 1.shell脚本说明:1行.指定使用shell 2行.注释行 3行.date 4行.命令who
# 2.解决环境变量:PATH=¥PATH:/home/test或使用./test1(绝对路径)
# 3.解决文件权限:chmod u+x test1
# 4.OK!实现完前三步就可以实现人生的第一个shell脚本了!(输入:test1或./test1)
退出脚本:exit $?保存退出状态码(exit status)
$ vi /home/test/test1.sh
#!/bin/bash
#testing the if statement
if pwd
then
echo "It worked"
fi
$ vi /home/test/test2.sh
#!/bin/bash
#testing the if-then-else
testuser=nouser
if grep $testuser /etc/passwd
then
echo "then bash files for user $testuser are:" is -a /home/$testuser/.b*
echo
else
echo "The user $testuser does not exist on this system."
echo
fi
$ vi /home/test/test3.sh
#!/bin/bash
#testing the if-then-else
testuser=nouser
if grep $testuser /etc/passwd
then
echo "The user $testuser exists on this system."
elif ls -d /home/$testuser/
then
echo "The user $testuser not exists on this system."
echo "But,$testuser has a directory."
else
echo "The user $testuser does not exist on this system."
echo "And,$testuser does not have a directory."
fi
// [codition]:方括号(左右有空格)定义了测试条件,作用等于test。
// 测试的条件:数值比较,字符串比较,文件比较。
// 双括号(()):允许使用高级数学表达式
// 双方括号[[]]:提供字符串比较。如:模式匹配
$ vi /home/test/test4.sh
#!/bin/bash
#testing case command
case $USER in
zld | zzz)
echo "welcome,$USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
*)
echo "Sorry,not login";;
esac
// break跳出本层循环,break 1,2,3跳出所有循环
// continue继续本层循环。
#!/bin/bash
for interface in $(ip link | grep ^[[:digit:]] | cut -d ':' -f 2 )
do
echo $interface"#######################"
ethtool -i $interface
echo "################################"
echo ""
done
while condition
do
command
done
//无限循环
while :
do
command
done
until condition
do
command
done
[ function ] funname [()]
{
action;
[return int;]
}
// 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
// 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255)
参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
#!/bin/bash
itisajoke(){
echo "pid: $$"
echo "params num:$#"
echo "params:$@"
echo $?
echo $!
}
itisajoke -a aaa -b bbb haha joke
#!/bin/bash
echo "script name:$0"
echo "$# parameters"
echo "parameters@:$@"
for para in $@
do
echo $para
echo "####"
done
echo "parameters*:$*"
for para in $*
do
echo $para
echo "####"
done
#!/bin/bash
names=(liliang lihaidong mayuying)
age=(20.2223 19.454 18)
nameNum=${#names[@]}
ageNum=${#age[@]}
if [ $nameNum -ge $ageNum ];then
minNum=$ageNum
else
minNum=$nameNum
fi
for i in $(seq 0 $(($minNum - 1)))
do
printf "%-10s %-4.2f\n" "${names[$i]}" "${age[$i]}"
done
for ((i=0;i<$minNum;i=i+1))
do
printf "%-10s %-4.2f\n" "${names[$i]}" "${age[$i]}"
done
// 和其他语言一样,Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。
Shell 文件包含的语法格式如下:
. filename # 注意点号(.)和文件名中间有一空格
或
source filename
// 创建两个 shell 脚本文件。
// test1.sh 代码如下:
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com
url="http://www.runoob.com"
// test2.sh 代码如下:
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com
#使用 . 号来引用test1.sh 文件
. ./test1.sh
# 或者使用以下包含文件代码
# source ./test1.sh
echo "菜鸟教程官网地址:$url"
接下来,我们为 test2.sh 添加可执行权限并执行:
$ chmod +x test2.sh
$ ./test2.sh
参考于:http://blog.csdn.net/StandMyGround/article/details/52485697