bash脚本实例

1、替换目录下文件或单个文件的字符串

#!/bin/bash
search_path=$1
old_str=$2
new_str=$3
grep $old_str -rl $search_path 
sed -i "s/$old_str/$new_str/g" `grep $old_str -rl $search_path`

使用方法:./replace.sh "/root/" "stdIO.H" "stdio.h"


2、将标准错误(2)的信息也重定向到文件中

/root/code/test >build.log 2>&1


3、遍历文件的各行,并且忽略某些特殊字符(;)开头的行

for item in `cat $config_path/wsp.cfg $config_path/bsp.cfg | grep  -v '^;'`
do
echo $item
done


4、判断环境变量是否设置

if [ -z $WINDRIVER ] 
then
export WINDRIVER=/rtools/windriver
fi


 

5、dos格式(含^M)转换为Unix格式
dos2unix -k .project 


6、bash命令执行结果判断
if [ $? -ne 0 ];then exit;fi


7、删除目录下特定的文件
for file in `find $config_path/../$item/PPC32sfgnu -name '*.keep*'`
   do
   echo $file
   rm -f $file
   done
----created by braveyly



5、dos格式(含^M)转换为Unix格式
dos2unix -k .project 


6、bash命令执行结果判断
if [ $? -ne 0 ];then exit;fi


7、删除目录下特定的文件
for file in `find $config_path/../$item/PPC32sfgnu -name '*.keep*'`
   do
   echo $file
   rm -f $file
   done
----created by braveyly

猜你喜欢

转载自blog.csdn.net/braveyly/article/details/17092181
今日推荐