例题:
shell1.sh 显示日期和时间。
#!/bin/bash
echo “current time is `date` ” //date 要加反引号
shell2.sh 显示文件名,并显示位置参数( 执行时带一个参数 )。(① $0 是一个特
殊的变量,它的内容是当前这个 shell 程序的文件名;② $1 是一个位置参数,位
置参数之间用空格分隔, shell 取第一个位置参数替换程序文件中的 $1,第二个
替换 $2,依次类推。 )
#!/bin/bash
echo “the program name is $0 ” //$0 是一个特殊的变数
echo “the first para is $1 ” //$1 是一个位置参数
echo “the program exit ” //执行时带一个参数 如./shell2.sh abcd
shell3.sh 判断并显示位置参数
#!/bin/bash
if [ - d “$1”];then
echo “$1 is directory ,existed ”
else
echo “$1 does not exist ,now create it ”
mkdir $1
echo “$1 is created ”
fi //执行时带一个参数
shell4.sh 问候用户
#!/bin/bash
user=`whoami`
case $user in
teacher)
echo “hello teacher ”;;
root)
echo “hello root ”;;
*)
echo “hello $user,welcome ”
esac
1、 求 1+2+3+...+100 的和是?
#!/bin/bash
SUM=0
I=0
while [ $I -le 100 ]
do
SUM=$((SUM+I))
I=$((I+1))
done
echo "由 1+2+3+...+100 的和是 :$SUM"
2、(基训十一 ) 在根目录下有四个文件 m1.txt,m2.txt,m3.txt,m4.txt,用 Shell
编程,实现自动创建 m1,m2,m3,m4 四个目录,并将 m1.txt,m2.txt,m3.txt,m4.txt
四个文件分别拷贝到各自相应的目录下。
#!/bin/bash
cd /
touch m1.txt m2.txt m3.txt m4.txt
I=1
while [ $I -le 4 ]
do
mkdir m$I
cp m$I.txt m$I
I=$((I+1))
done