S1:打印一个变量a
#有符号#!来指定该脚本文件的解析程序
#!/bin/sh
#设置一个变量a,不需要预先对变量经行声明
a=”hello world”
#打印变量a
echo “A is :”
echo $a
num=2
echo “this is the $numnd”
打印出“this is the”
num=2
echo “this is the ${num}nd”
打印出“this is the 2nd”
S2:默认变量$#、$*、$1、$2……的使用
#!/bin/sh
echo "number of vars:"$#
echo "values of vars:"$*
echo "value of var1:"$1
echo "value of var2:"$2
echo "value of var3:"$3
echo "value of var4:"$4
s3:局部变量的使用
#!/bin/bash
hello="var1"
echo $hello
function func1 {
echo $hello
s4:实现对两个脚本参数的比较
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
fi
if [ $a -eq $b ] ; then
else if [ $a -gt $b ]
fi
s5:文件测试操作符使用
#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder"
[ -f "$folder" ] || echo "this is not file"
s6:根据输入的参数值,创建一个名字为参数值的文件夹
#!/bin/bash
DIR=$1
#if
if [ "$DIR" = "" ]
then
fi
echo "dir" $DIR
if [ -d $DIR ]
then
else
fi
s7:循环结构的使用1
#!/bin/bash
for day in Sun Mon Tue Wed Thu Fri Sat
do
done
s8:循环结构的使用2
#!/bin/bash
for day in "Sun Mon Tue Wed Thu Fri Sat"
do
done
s9:统计当前目录下的文件数
#!/bin/bash
counter=0
for files in *
do
done
echo "There are $counter files in `pwd` we need to process"
s10:将用户输入的数字按倒序输出
#!/bin/bash
echo -n "Pleasw enter number : "
read n
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
done
echo
s11:移动一个文件,如果目标文件存在该文件,则监视该文件,直到文件被删除后才移动该文件
#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ]
then
fi
if [ -e $2 ]
then
fi
if [ ! `mv $1 $2` ]
then
else
fi
s12:根据输入的字符来判断是大写字母还是小写字母还是数字或是其他
#!/bin/bash
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
Esac