您的位置:首页 > 运维架构 > Shell

bash脚本实例

2013-12-03 11:12 363 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: