Shell .sh,for, if, 变量的使用

#!/bin/sh 

#使用变量(等号两边不能有空格)
#var="hello"
#echo "$var"


#for 遍历有file(注意当前路径)("$fileList" 和 $fileList 是有区别的,前者是用字符串包含了所有的路径)
#fileList=`find . -name '*.tga'`
#for file in $fileList
#do
#echo $file
#done

:<<!
#if 语句的使用 (要在中括号[ ]中的条件两侧加上空格)
fileList=`find . -name '*.tga'`
for file in $fileList
do
	fileW=`identify -format "%w" $file`
	fileH=`identify -format "%h" $file`
	if [ $fileW == $fileH ]
	then
		echo "$fileW" "$fileH"
		echo "$file"
	fi
done
!




注意的地方:
1. 因为上面的.sh文件是在Window编写的,想要.sh文件在CygWin可以正常运行,那么换行符要转为 UNIX 的 LF。


2. 在使用变量的时候,等号两边不能有空格。


3. fileList=`find . -name '*.tga'` 
3.1 for file in $fileList
3.2 for file in "$fileList"
3.1 和 3.2 的写法是有区别的,
3.1 是1个1个路径进行遍历,for 循环是多次的,而3.2   "$fileList" 这种写法就是把所有find出来的路径都塞到一个字符串中,for循环就只有1次。


4  在写if 语句的时候,[] 条件两侧加上空格



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值