#!/bin/sh
echo "***********该程序展示*号能够匹配任意个字符******"
read xx
case $xx in
a*)
echo "$xx is a*"
;;
b*)
echo "$xx is b*"
;;
?)
echo "$xx is 1 char"
;;
??)
echo "$xx is 2 char"
;;
???)
echo "$xx is 3 char"
;;
*)
echo "$xx is *"
;;
esac
#!/bin/sh
echo "********该程序能给指定的文件加上指定的权限*****"
if [ $# -ne 2 ];then
echo "Need 2 parameters!"
exit 2
fi
file=$1
mode=$2
case $mode in
r)
chmod u+r $file
;;
w)
chmod u+w $file
;;
x)
chmod u+x $file
;;
*)
echo "error!"
exit 1
esac
#!/bin/sh
echo "********该程序演示模式匹配符[],若指定字符包含在【】中,则条件成立,如本例中输入a1是type 1,输入a10是others"
read xx
case $xx in
a[123])
echo "type 1 "
;;
a[456])
echo "type 2"
;;
a[789])
echo "type 3"
;;
*)
echo "others"
esac
#!/bin/sh
echo "*******该程序演示模式匹配符|,若指定字符串等于|所分割开的任意的一个字符串,则条件成立"
read xx
case $xx in
y|Y|yes|YES)
echo "agree!"
;;
n|N|no|NO)
echo "reject!"
;;
*)
echo "unknown!"
esac
#!/bin/bash
for kk in $@
do
echo $kk
done
#!/bin/bash
IFS=:
declare -i count =0
for kk in $PATH
do
echo $kk
((count++))
done
echo "There are $count pahts."
#!/bin/sh
declare -i sum=0
for((k=1;k<100;k++))
do
sum=sum+k
done
echo "=$sum"
#!/bin/sh
declare -i n
read -p "n=" n
for((k=1;k<=n;k++))
do
for((h1=n;h1>=k;h1--))
do
echo -n " "
done
for((h2=1;h2<=2*k-1;h2++))
do
echo -n "*"
done
echo
done
###该程序打印当前目录下前10个文件名
#!/bin/sh
echo "************该程序打印当前目录下前10个文件名**********"
declare -i count=0
for file in *
do
echo $file
count=$count+1
if [ $count -ge 10 ];then
break
fi
done
echo "There are top 10 filenames"
#!/bin/sh
echo "**************该程序以长格式显示当前目录下的可执行文件"
for file in *
do
if ! [ -x $file ];then
continue
fi
echo "ls -l $file"
done
echo "There files can be executed"
#!/bin/sh
echo "格式1:
function 函数名{
}
格式2:
函数名()
{
}
如例子所示。
"
fun()
{
echo "This is fun()"
echo "The first patameters is $1"
}
fun a b c
#!/bin/sh
echo "*************判断用户是否是root用户************"
username=$LOGNAME
if [ $username = "root" ];then
echo "you are root!"
else
echo "you are not root"
fi
#!/bin/sh
echo "**********该程序用户判断通过参数指定的文件的类型************"
file=$1
if [ -f $file ];then
echo "$file is a normal files"
elif [ -d $file ];then
echo "$file is a directory"
elif [ -b $file ];then
echo "$file is a block device file."
elif [ -c $file ];then
echo "$file is a character device file"
else
echo "I don't know $file"
fi
#!/bin/sh
select kk in Linux Windows OS2 Macintosh
do
echo "REPLY = $ REPLY"
echo "kk=$kk"
done
# ! /bin/sh
declare -i a1=0 a2=0 a3=0 a4=0
select kk in Linux Windows OS2 MAC Quit
do
case $kk in
Linux) echo "Linux is selected!"
a1=$a1+1
;;
Win*) echo "Windows is selected!"
a2=$a2+1
;;
OS2) echo "OS2 is selected!"
a3=$a3+1
;;
MAC) echo "MAC is seclected!"
a4=$a4+1
;;
Quit) break
;;
*) echo "Error! Try again!"
;;
esac
done
echo "Linux is selected $a1 times"
echo "win is selected $a2 times"
echo "OS2 is selected $a3 times"
echo "MAC is selected $a4 times"
#!/bin/sh
touch ~/桌面/test.txt
declare -i aa
for file in *
do
cat $file >> ~/桌面/test.txt
aa=aa+1
done
echo "The total num is $aa"
#!/bin/sh
echo "**********计算通过输入整数的阶乘*******"
declare -i kk=1 fac=1 n
read -p "n=" n
until [ $kk -ge $n ]
do
kk=$kk+1
fac=$fac*$kk
done
echo "$n! = $fac"
#!/bin/sh
echo "****该程序给指定文件加上行号进行显示,文件名以输入转向形式提供**** /n"
echo "*********usage: A < B "
declare -i num=0
while read line
do
num=$num+1
echo "$num:$line"
done
Linux shell 基本语法实例程序
最新推荐文章于 2024-09-17 20:39:29 发布