shell脚本基础学习(一)

**基本指令的基础:

判断语句if的使用

#!/bin/bash
if[condition]
then 
   command1
elif condition2
then 
  command3
fi

condition:

  1. 数值比较:
命令意义
-eq相等
-ge大于等于
-gt大于
-le小于等于
-lt小于
-ne不等于
  1. 字符串比较(!!因为><和重定向符号相同用\隔开):
命令意义
str1 = str2相等
!=判断字符串是否相同
<小于
>大于
-n str1长度是否非0
-z str1长度是否为0
  1. 文件比较:
命令意义
-d file是否存在并且是一个目录
-e是否存在
-f是否存在并且是一个文件
-s是否存在并且非空
more

for循环的使用(expC语言风格)

for var in list
do
 commands
done
  1. 读取list中的值比如:(请注意转义字符和单引号的使用)
for test in i don\'t konw "this'll" work
do
  echo the next is $test
done
#或者将list赋值给变量
#---------
list =  i don\'t konw "this'll" work
for test in list
#---------
$
the next is if 
the next is i
the next is don't
the next is know
the next is this'll
the next is work
$
  1. 从命令读取值
#reading values from a file
#IFS= $'\n';将会忽略空格和制表符
file = "test"
for test in `cat $file`;do 
echo "$test"
done
  1. 遍历文件夹和文件
for file in /home/lucy/test/*
do
	if [ -d "$file" ]
	then
		echo "$file" is a direct
	elif [ -f "$file" ]
	then 
		echo" $file is a file"
	fi
done

while循环的使用

while [condition]
do
	command
done
#condition和if中的判断条件相似
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值