如何在shell脚本中替换变量

时间:2022-04-26 11:31:58

In the below script,I want to add the file name as prefix to each output line generated by grep. I dont know why this is not replacing the filename with $file,I am getting the $file as the prefix.can anyone help me on this

在下面的脚本中,我想将文件名作为前缀添加到grep生成的每个输出行。我不知道为什么这不是用$ file替换文件名,我得到$ file作为前缀。任何人都帮我这个

function traverse() {
  for file in "$1"/*
  do
    if [ ! -d "${file}" ] ; then
      if [ ${file: -2} == ".c" ]
      then
        ./sed.sed "$file"  >  latest_log.txt #To remove all the comments
        grep -nir "$2"   latest_log.txt >> output.log #grep matched lines
        sed -i "s/^/$file/" output.log > grepoutput3.txt #prefix filename($file here)
        echo "${file} is a c file"
      fi
    else
      traverse "${file}" "$2"
    fi
  done
}

function main() {
  traverse "$1" "$2"
}

main "$1" "$2"

The below line should add the filename as prefix,but $file not replacing,Apart from that whole script is working fine.

下面的行应该添加文件名作为前缀,但$ file不替换,除了整个脚本工作正常。

sed -i "s/^/$file/" ex.txt > grepoutput3.txt

EX: search for "welcome" in all .c files of a folder.Take first_file.c is one.

EX:在文件夹的所有.c文件中搜索“welcome”。获取first_file.c是一个。

FIRST_FILE.C

welcome here

/* welcome here */

/* 欢迎到这里 */

//welcome here

welcome here2

Expected output

/DIR/FIRST_FILE.C:1: welcome here

/DIR/FIRST_FILE.C:1:欢迎来到这里

/DIR/FIRST_FILE.C:4: welcome here2

/DIR/FIRST_FILE.C:4:欢迎来到这里2

with my script ,output is

用我的脚本,输出是

1: welcome here

1:欢迎来到这里

4: welcome here2

4:欢迎来到这里2

So,I am trying to prefix the file name (/DIR/FIRST_FILE.C) to each line.we can fetch that filename from $file,But sed is not interpreting it .

所以,我试图将文件名(/DIR/FIRST_FILE.C)添加到每一行。我们可以从$ file获取该文件名,但是sed没有解释它。

2 个解决方案

#1


0  

Nowhere in your script is any code that creates ex.txt. Is that intended? To debug this, run your script under set -x near the beginning.

您的脚本中没有任何代码可以创建ex.txt。这是有意的吗?要调试它,请在开头附近的set -x下运行脚本。

Also worth noting: you have tagged this question "sh" (not bash) but

另外值得注意的是:你已经标记了这个问题“sh”(不是bash)但是

if [ ${file: -2} == ".c" ]

is full of nonportable bashisms: the string equality operator is = and the ${file: -2} syntax is not understood by a POSIX sh. An equivalent sh code would use

充满了不可移植的bashisms:字符串相等运算符是=并且POSIX sh不理解$ {file:-2}语法。将使用等效的sh代码

case $file in
(*.c) ...;;
esac

#2


0  

For the filesystem traversal: Use find!

对于文件系统遍历:使用find!

find FOLDER -type f -name '*.c' -exec bash yourscript.sh {} \;

In yourscript.sh you can access the filename in $1.

在yourscript.sh中,您可以访问$ 1中的文件名。

I could help further with implementing that script but it is pretty unclear what you are trying to do.

我可以帮助进一步实现该脚本,但目前还不清楚你要做什么。

#1


0  

Nowhere in your script is any code that creates ex.txt. Is that intended? To debug this, run your script under set -x near the beginning.

您的脚本中没有任何代码可以创建ex.txt。这是有意的吗?要调试它,请在开头附近的set -x下运行脚本。

Also worth noting: you have tagged this question "sh" (not bash) but

另外值得注意的是:你已经标记了这个问题“sh”(不是bash)但是

if [ ${file: -2} == ".c" ]

is full of nonportable bashisms: the string equality operator is = and the ${file: -2} syntax is not understood by a POSIX sh. An equivalent sh code would use

充满了不可移植的bashisms:字符串相等运算符是=并且POSIX sh不理解$ {file:-2}语法。将使用等效的sh代码

case $file in
(*.c) ...;;
esac

#2


0  

For the filesystem traversal: Use find!

对于文件系统遍历:使用find!

find FOLDER -type f -name '*.c' -exec bash yourscript.sh {} \;

In yourscript.sh you can access the filename in $1.

在yourscript.sh中,您可以访问$ 1中的文件名。

I could help further with implementing that script but it is pretty unclear what you are trying to do.

我可以帮助进一步实现该脚本,但目前还不清楚你要做什么。