【Shell】记录shell的应用例子

在Makefile中调用shell脚本需要注意:
1. 如果需要调用多个shell,必须在每行最后加上 \  表示该行还没结束,如果要命令在同一目录下执行,则 在行尾加 && \,这是因为在Makefile中,多行shell都视为一行执行;
2.在引入的shell中不要引入变量,因为Makefile将该变量试作命令,报错:
/bin/sh: line 4: ***: command not found;
3.如果引入的shell脚本复杂,可以写在一个独立的shell脚本中,然后在Makefile中调用这个脚本;

shell例子:

1.查找包含关键字为string并且后缀为.bin的文件,并且删除关键字string
#!/bin/sh
val=$(find . -maxdepth 1 -name "*.bin" | grep string )
val1=`echo $val | sed 's/factory_mode-//g' | sed 's/.\///g'`
echo $val1


2.查找输入参数$1开头的bin文件,并修改文件名为带参数$2, 如,test.bin修改为test-test.bin

#!/bin/bash
echo $1
echo $2
for eachfile in `ls $1*.bin`
do
filename=${eachfile%.bin}
echo "mv $filename.bin to $filename-$2.bin"
mv $filename.bin $filename-$2.bin
done

或者用一条命令搞定:

find . -type f -name "$(PID)_$(RELEASE)*.bin" | xargs rename "s/.bin/-test.bin/"
不过要注意rename命令分Perl版本和Linux C版本,Perl版本支持正则表达式

猜你喜欢

转载自blog.csdn.net/vickytong1018/article/details/73288910