shell脚本--语法基础

shell脚本,可以封装一系列命令,组合成一个命令,简化我们日常在linux下的工作量,这是我学shell最初的一个原因。当然shell还能敢其他事情。

shell脚本if else 语句的使用

#!/bin/bash
NUM1=100
NUM2=200
if(($NUM1>$NUM2));then

	echo "num1>num2"

else
	echo "num1<num2"
fi

shell脚本for循环的使用
seq 1 15 :是linux下的命令。从1到15.

#!/bin/bash
for i in `seq 1 15`
do
	echo "数字  $i"
done

shell脚本while循环使用

while [ $i -le 100 ] 与 while [ $i -lt 100 ]
-le:小于等于,-lt:小于

#!/bin/bash
i=0
while [[ $i -lt 100 ]]
do
	echo "数字 $i"
	i=`expr $i + 1`
done

shell脚本file文件操作

检查文件是否存在,不存在创建,存在输出内容

#!/bin/sh
NDK=/root/text.txt
if [ ! -f $NDK ]; then
	mkdir -p /root/wangyi
else
	echo "目录已经存在"
	cat $NDK
fi

shell脚本读取文件内容并逐行输出

#!/bin/bash
while read line
do
	echo $line
done</root/text.txt

shell脚本字符串操作:

#!/bin/bash
a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi

if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

shell脚本压缩文件

#!/bin/bash
for i in `find /root -name "*.sh"`
do
	tar -czf wangi.tgz $i
done

shell脚本函数的使用

#!/bin/bash
aa="this is aa"
bb="this is bb"
function test(){
	echo $1
	cc="this. is cc"
	 dd="this is dd"
	echo $cc
	echo "我是一个函数"
}
test 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值