shell脚本基础demo

这里只是一些简单的shell脚本的基础例子,适合入门的朋友练练手

#! /bin/sh
#测试echo、位置变量
ls
echo hello
echo "\$* is $*"
echo "\$@ is $@"
echo "\$# is $#"
date
################################
#! /bin/bash
#测试三种引号
log=Sunday
#双引号阻止shell对大多数特殊字符进行解释
echo "Today is $log"
#单引号阻止shell对所有字符进行解释
echo 'Today is $log'
#倒引号修饰的命令将会被执行,输出结果作为这个表达式的值
echo "Today is `date`"
#################################
#! /bin/bash
#shell脚本是一种“弱类型”的语言,它并不知道num中保存的是
#一个数字。可以用$[]来高速shell应该对其中的表达式求值
num1=1+2
num2=$[1+2]
echo $num1 $num2
#################################
#! /bin/bash
#shell脚本是一种“弱类型”的语言,它并不知道num中保存的是
#一个数字。可以用$[]来告诉shell应该对其中的表达式求值
num1=1+2
num2=$[1+2]
echo $num1 $num2
#也可以用let表达式,用于计算整数表达式的值
let num=$num1+1
echo $num
#expr的命令允许的表达式更复杂,注意数字和+之间有空格
expr 2 + 2
######################################
#! /bin/bash
#if语句,格式很重要,if后有空格,[]前后有空格,“=”前后有空格,
#“;”代表一行结束,所以可以在后面写then,否则换行
echo "Enter passwd"
read passwd
if [ "$passwd" = "mypasswd" ];then
	echo "passwd is correct!"
elif test "$passwd" = "miss";then
	echo "passwd is missing"
else
	echo "passwd is wrong"
fi
####################################
#! /bin/bash
#条件测试,测试某个程序的返回值
#需要另建一个testscript文件如下
# code   #! /bin/sh
#        exit $@

if ./testscript -1;then
	echo "testscript exit #1"
fi
if ./testscript 0;then
	echo "testscript exit 0"
fi
if ./testscript 1;then
	echo "testscript exit 1"
fi

###################################
#!/bin/sh
#test和[]可以互换

#判断登陆shell是不是sh
if test "$SHELL" = "bin/bash";then
	echo "shell is /bin/bash"
fi
#判断是不是一个文件
if [ -f $0 ];then
	echo "$0 is a file"
fi
#判断是否可执行
if [ -x "$0" ];then
	echo "$0 can be x"
fi
#判断是否有值
if [ -n $0 ];then
	echo "$0 has value"
fi
################################
#!/bin/sh
#用于数字的比较
##eq 如果相等返回真
##ne 如果不等返回真
##lt 如果前者小于后者返回真
##le 如果前者小于或等于后者返回真
##gt 如果前者大于后者返回真
##ge 如果前者大于或等于后者返回真
num=5
if [ $num -eq 5 ];then
	echo "num=5"
fi
####################################
#!/bin/sh
#复合表达式
#!非
#-a与
#-o或
#也可以用&&和||,这样看起来更清晰,但效率较低
num1=5
num2=3
if [ $num1 -eq 5 -a $num2 -eq 4 ];then
	echo "sucess"
fi
if [ $num1 -eq 5 ] && [ $num2 -eq 3 ];then
	echo "sucess2"
fi
#####################################
#! /bin/bash
#和C不同,case必须有“;;”用于break,"*"代表匹配所有字符串
case "$1" in
	start)
	echo start;;
	stop)
	echo stop;;
	*)
	echo hello;;
esac
####################################
#!/bin/sh
#计算1到输入的count的累加
while read number
do
	sum=0
	count=1
	if [ $number -gt 0 ];then
		while [ $count -le $number ]
		do
			sum=$[$sum+$count]
			count=$[$count+1]
		done
		echo $sum
	fi
done
#################################
#!/bin/sh
#计算1到输入的100的累加
#until 和while的判断条件相反
sum=0
count=1
#until ! [ $count -le $number ]
until [ $count -gt 100 ]
do
	sum=$[$sum+$count]
	count=$[$count+1]
done
echo $sum
######################################
#! /bin/bash
#测试for
echo "$# file(s) to list"

for file in $@
do 
	ls -l $file
done
#输出列表里的文件名
for file in `ls`
do
	echo $file
done	
################################
#!/bin/sh
#输出列表的数,seq工具可产生列表,要用倒引号扩起来
#for i in 1 2 3 4 5
for i in `seq 5`
do
	echo $i
done	
##################################
#! /bin/bash
#read 语句 如果不提供变量名,那么读取的信息将存放在REPLY变量中
read firt second
echo $firt 
echo $second
#read 常用来在输出一段内容后暂停,等待用户继续
echo "press <ENTER> to continue"
read
echo "END"
####################################
#! /bin/bash
#exit用于强行退出一个脚本,并向调用这个脚本的进程返回一个整数
#trap用于捕捉信号量,并忽视这个信号量
trap 'echo "input quit to exit"' INT
#提示用户输入quit退出程序
while [ "$input" != 'quit' ]
do 
	read input
done
exit 0
##########################################
#! /bin/bash
#在trap中使用了复合命令echo "Goodbye";exit
#即先执行echo Goodbye显示提示信息,再exit退出脚本
trap 'echo "Goodbye";exit' EXIT
echo "input quit to exit"
#提示用户输入quit退出程序
while [ "$input" != 'quit' ]
do 
	read input
done
exit 0
#########################################
#! /bin/bash
#自己编写命令delete,用这个删除文件时,可以删除到回收站。
#将编写的命令移动到/bin里面,就可以当做普通命令使用了
if [ ! -d ~/.trash ];then
	mkdir ~/.trash
fi
if [ $# = 0 ];then
	echo "Usage: delete file1[file2,file3...]"
else
	echo "You are about to delete these files"
	echo "$@"
	echo -n "Are you sure to do that?[Y/n]"
	read reply
	if [ "$reply" = "Y" ] || [ "$reply" = "y" ];then
		for file in $@
		do 
			if [ -f $file ]||[ -d $file ];then
				mv -b $file ~/.trash
			else
				echo "$file:No such file or directory"
			fi
		done
	fi
fi
########################


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值