&引用;“调试”;bashshell脚本:转义字符、传递参数

&引用;“调试”;bashshell脚本:转义字符、传递参数,bash,debugging,shell,command-line,Bash,Debugging,Shell,Command Line,我正在尝试编写一个bashshell脚本(这是我没有太多经验的),以便在通过ssh操作的Unix机器上搜索C代码 这是我脚本中的代码: #! /bin/bash find . \( -name '*.c' -o -name '*.h' \) -exec grep -n '<SEARCH_STRING>' {} + | sed -e "s/^\(\..*\.c\):\([0-9]*\):[ \t]*\(.*\)$/\1 :: \2\n\3\n/" | sed -e "s/^\(\..

我正在尝试编写一个bashshell脚本(这是我没有太多经验的),以便在通过ssh操作的Unix机器上搜索C代码

这是我脚本中的代码:

#! /bin/bash

find . \( -name '*.c' -o -name '*.h' \) -exec grep -n '<SEARCH_STRING>' {} + | sed -e "s/^\(\..*\.c\):\([0-9]*\):[ \t]*\(.*\)$/\1 :: \2\n\3\n/" | sed -e "s/^\(\..*\.h\):\([0-9]*\):[ \t]*\(.*\)$/\1 :: \2\n\3\n/" | sed ''/<SEARCH_STRING>/s//`printf "\033[31m<SEARCH_STRING>\033[0m"`/'' | less -R
#/bin/bash
找到\(-name'*.c'-o-name'*.h'\)-exec grep-n''{}+\sed-e“s/^\(\..*\.c\):\([0-9]*\:[\t]*\(.*\)$/\1::\2\n\3\n/“\sed-e”s/^\(..*.h\):([0-9]*\):[\t]*(*\):[\t]/\1:\2\n\n\3\n/”;sed-e“s”s/'s/'m/'033m/'
请注意,这仅仅是我用来表示搜索字符串的内容,当然我并不是在逐字逐句地搜索它

我意识到这有两个问题。首先,当我在命令行中键入底线(“脚本”不带#!/bin/bash标题)时,它实际上工作得很好,并给出了我想要的结果。我知道在将其制作成bash脚本时,我应该转义某些字符,但我尝试了转义几个字符(组合)字符,它总是给我相同的错误:

find: missing argument to `-exec'
./code_search.sh: line 4: <SEARCH_STRING>: command not found
sed: -e expression #1, char 43: unterminated `s' command
printf: usage: printf [-v var] format [arguments]
./code_search.sh: line 8: \033[31m<SEARCH_STRING>\033[0m: command not found
sed: -e expression #1, char 46: unterminated `s' command
find:“-exec”缺少参数
./code_search.sh:第4行::未找到命令
sed:-e表达式#1,字符43:unterminated's'命令
printf:usage:printf[-v var]格式[参数]
./code_search.sh:第8行:\033[31m\033[0m:未找到命令
sed:-e表达式#1,字符46:unterminated's'命令
此外,我意识到我不应该将搜索字符串硬编码到我的脚本中,因为将搜索字符串作为参数提供给脚本要方便得多,但我也确实不知道如何做到这一点

正如我所说,我没有很多编写bash脚本的经验,我试着编写这个脚本来帮助我进行编码工作。但是,我不能再花更多的时间调试这个脚本,因为这不是我老板付钱让我做的。我已经问过我的同事,但他们都不能帮助我

我知道自己不花很多精力去学习更多关于bashshell脚本的知识有点便宜,但是就像我说的,我真的不能抽出时间。有人能帮我完成这个脚本吗


事先非常感谢!

以这种方式,它不会给出任何错误

#! /bin/bash

searchstring=$1

find . \( -name '*.c' -o -name '*.h' \) -exec grep -n "$searchstring" {} + \ 
        | sed -e "s/^\(\..*\.c\):\([0-9]*\):[ \t]*\(.*\)$/\1 :: \2\n\3\n/" \
        | sed -e "s/^\(\..*\.h\):\([0-9]*\):[ \t]*\(.*\)$/\1 :: \2\n\3\n/" \
        | sed ''/"$searchstring"/s//`printf "\033[31m""$searchstring""\033[0m"`/'' \
        | less -R

但是,这并不意味着它可以工作。如果没有一些示例输入和所需的输出,就不可能进一步帮助您。例如,第三个
sed
对我来说似乎是错误的。

这是因为,在最后一个sed命令中,字符<没有转义:双单引号仅表示空字符串“”;而<被解释为file重定向时,shell尝试从文件“SEARCH_STRING”中读取


感谢您的帮助!示例输入类似于
/code\u search.sh isValid
,示例输出类似于
/users/user/code/codefile1.c::15 boolean isValid,activatesCheck;/users/user/code/codefile1.c::321 if(!isValid){
不过,我在运行它时仍然遇到错误;很明显,find有问题:
find:path必须在expression用法之前:find[-H][L][P][path…][expression]
感谢您的帮助!我已经尝试更改了您的建议(组合和单独),但我仍然无法使脚本正常工作。每当我更改某些内容时,仍然会出现错误;很明显,查找时存在问题:
find:path必须在表达式之前使用:find[-H][L][P][path…][expression]
sed ''/<SEARCH_STRING>/s//`printf "\033[31m<SEARCH_STRING>\033[0m"`/'' 
sed '/<SEARCH_STRING>/s//'`printf "\033[31m<SEARCH_STRING>\033[0m"`'/' 
find . \( ... \) | xargs grep -n '<SEARCH_STRING>'