嵌入式linux学习之shell 操作例程

目录

目录

1、替换字符串为指定文件

2、在指定字符串前插入字符

3、查询指定字符串,并返回行号

4、扫描指定文件夹

5、递归方式在指定目录下搜索目标文件

6、变量的引用

7、监控指定文件的大小

8.查找并替换当前目录的指定文件为指定文件

9.查找当前目录的指定文件,并将文件(带路径)copy到指定目录

10、在当参数1指定的目录,查到参数2指定的文件,然后用参数3指定的文件去替换参数2指定的文件

11、搜索参数1指定路径下的文件,然后其中的参数2的字符串替换为参数3的字符串


在bash中,$( )` `(反引号)都是用来作命令替换的,命令替换与变量替换差不多(在操作上,这两者都是达到相应的效果,但是建议使用$( ) ),二者都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行

${}:变量替换,一般情况下,$var与${var}是没有区别的,但是用${ }会比较精确的界定变量名称的范围

1、替换字符串为指定文件

sed -i -e '/para1/{r file2' -e 'd}' file1

将file1中的字符串para1,替换为file2的内容,

命令中是单引号,不是反引号

2、在指定字符串前插入字符

sed -i '/para1/i para2\\' file

在文件file的para1之前插入para2

3、查询指定字符串,并返回行号

line_num=$(sed -n -e "/para1/=" file)
if [ $line_num ]; then
    echo $line_num
    echo "done"
else
    echo "NULL done"
fi

4、扫描指定文件夹

mp_flag=0

function scandir(){
#work_dir=$1
#echo $1
for typefile in $(ls $1)
do
#echo $typefile
typefn=$(basename $typefile)
typeprefix=${typefn}
#echo $typeprefix

if [ "$typeprefix" = 'mp_test.dat' ] #判断是否存在mp_test.dat文件
then
echo "scan mp_test ok"
#echo $typeprefix
mp_flag=1
fi
done

}

#

media_dir="/media/" #指定扫描路径为media
for dirlist in $(ls $media_dir) #遍历目录
do
#echo $dirlist
scandir $media_dir$dirlist #扫描各个子目录

done

5、递归方式在指定目录下搜索目标文件

#!/bin/sh
dir=
function find_file()
{
	#work_dir=$1
	#echo $1
	for typefile in $(ls $1)                        
	do                                  
        #echo $typefile                     
        typefn=$(basename $typefile)        
        typeprefix=${typefn}                
        #echo $typeprefix                   
                                            
        if [ "$typeprefix" = $2 ]
        then                                
        echo "$1/$2"                    
        #echo $typeprefix
	dir=$1/$2                  
        fi                                  
	done
	return $?  
}

function find_dir()
{
    for file in `ls -a $1`
    do
        if [ -d $1"/"$file ]
        then
            if [[ $file != '.' && $file != '..' ]]
            then
                find_dir $1$file $2
            fi
        else
            #echo $1"/"$file
		find_file $1 $2
		ret=
		if [ $dir ];
		then
			#echo "find target"
			return
		fi
		
        fi
    done
}
echo $1 $2
find_dir $1 $2
#echo $dir

6、变量的引用

参考(74条消息) sed 引用shell变量_江湖书苑-CSDN博客_sed 引用变量

1. 如果sed 命令使用的是 单引号,引用变量要使用 $+单引号+双引号 的方式。
例子如下

addrs=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
sed   's/localhost:1935/'"$addrs"':1935/g'   filename		


2. 如果sed 命令使用的是 双引号,
可以使用 反引号 来直接执行命令。

sed   "s/localhost:1935/`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`:1935/g" filename


或者使用 $() 来直接执行命令。

sed   "s/localhost:1935/$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"):1935/g"  filename

7、监控指定文件的大小

/record目录下有很多skylogX.log的文件,本程序自动监控X编号最大的文件的大小

#n=$(ls /record | grep log |awk '{ print $5 }')
#echo $?
n=$(ls /record | grep log | wc -l)
m=`expr $n - 1`
echo ${m}

#exit

while [ 1 ] ;do
        #do du  /record/skylog$m.log| awk '{ if($1 > 512000){flag=1;printf"rm it\n"}}'
        file_size=`du /record/skylog$m.log|awk '{ if($1 > 500){print $1}}'`
        echo $file_size
        if [ $file_size > 512000 ]; then
                echo "file too big"
                break
        fi
        sleep 2
done
echo "leave monitor file"

8.查找并替换当前目录的指定文件为指定文件

#!/bin/bash
file_list=$(find -name $1)
for file in $file_list;
    do 
    echo $file
    cp $2 $file
    done

参数1为指定文件名

参数2为指定文件

在当前目录查找参数1指定的文件,并用参数2指定的文件替换找到的参数1指定的文件

9.查找当前目录的指定文件,并将文件(带路径)copy到指定目录

#!/bin/bash
file_list=$(find -name $1)
for file in $file_list;
    do 
    echo $file
    mkdir -p $2/$(dirname $file)
    cp $file $2/$file
    done

参数1为目标文件名

参数2位目标文件夹

在当前目录查找参数1指定的文件,并将结果(带路径)copy到参数2指定的文件夹

10、在当参数1指定的目录,查到参数2指定的文件,然后用参数3指定的文件去替换参数2指定的文件

#!/bin/bash
#参数1为目标文件夹
#参数2为目标文件名
#参数3为欲替换的文件名(替换参数2指定的文件)
#功能:在当参数1指定的目录,查到参数2指定的文件,然后用参数3指定的文件去替换参数2指定的文件
file_list=$(find $1 -name $2)
for file in $file_list;
    do 
    echo $file
    #mkdir -p $2/$(dirname $file)
    #cp $file $2/$file
    cp $3 $file
    done

11、搜索参数1指定路径下的文件,然后其中的参数2的字符串替换为参数3的字符串

#!/bin/bash
#功能:搜索参数1指定路径下的文件,然后其中的参数2的字符串替换为参数3的字符串
# 函数:搜索和替换文本(字符替换)
search_and_replace() 
{
    find_path=$1  # 搜索的目录路径
    search_text=$2  # 要搜索的文本
    replace_text=$3  # 替换文本
 
    # 在find命令的-exec参数中使用sed进行文本替换
    find "$find_path" -type f -exec sed -i "s/$search_text/$replace_text/g" {} \;
}
 
# 使用示例
# 设置搜索目录,搜索文本,替换文本
search_path=$1  # 当前目录
search_for=$2  # 要搜索的文本
replace_with=$3  # 替换文本
 
# 调用函数进行搜索和替换
search_and_replace "$search_path" "$search_for" "$replace_with"

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值