[root@www scripts]# echo $firstname $lastname<==确认了,这两个变量并不存在喔!
[root@www scripts]# sh sh02.sh
Please input your first name: VBird<==这个名字是鸟哥自己输入的
Please input your last name: Tsai
Your full name is: VBird Tsai<==看吧!在 script 运行中,这两个变量有生效
[root@www scripts]# echo $firstname $lastname<==事实上,这两个变量在父程序的 bash 中还是不存在的!
[root@www scripts]# source sh02.sh
Please input your first name: VBird
Please input your last name: Tsai
Your full name is: VBird Tsai
[root@www scripts]# echo $firstname $lastname
VBird Tsai <==嘿嘿!有数据产生喔!
[root@www scripts]# vi sh07.sh#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2009/02/17 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \
&& exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> $1"
echo "The 2nd parameter ==> $2"
运行结果如下:
[root@www scripts]# sh sh07.sh theone haha quot
The script name is ==> sh07.sh <==档名
Total parameter number is ==> 3 <==果然有三个参数
Your whole parameter is ==> 'theone haha quot' <==参数的内容全部
The 1st parameter ==> theone <==第一个参数
The 2nd parameter ==> haha <==第二个参数
[root@www scripts]# vi sh08.sh#!/bin/bash
# Program:
# Program shows the effect of shift function.
# History:
# 2009/02/17 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift # 进行第一次『一个变量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 # 进行第二次『三个变量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
这玩意的运行成果如下:
[root@www scripts]# sh sh08.sh one two three four five six<==给予六个参数
Total parameter number is ==> 6 <==最原始的参数变量情况
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5 <==第一次偏移,看底下发现第一个 one 不见了
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2 <==第二次偏移掉三个,two three four 不见了
Your whole parameter is ==> 'five six'
单层、简单条件判断式
如果你只有一个判断式要进行,那么我们可以简单的这样看:
if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的命令工作内容;
fi<==将 if 反过来写,就成为 fi 啦!结束 if 之意!
date_d=$(echo $date2 |grep '[0-9]\{8\}') # 看看是否有八个数字 if [ "$date_d" == "" ]; then echo "You input the wrong date format...." exit 1 fi