简体   繁体   English

我希望我的脚本从字面上回显“ $ 1”到文件中

[英]I want my script to echo “$1” into a file literally

This is part of my script 这是我脚本的一部分

#!/bin/bash

echo "ls /SomeFolder | grep $1 | xargs cat | grep something | grep  .txt | awk '{print $2}' | sed 's/;$//';" >> script2.sh

This echos everything nicely into my script except $1 and $2. 除了$ 1和$ 2,这一切都很好地呼应了我的脚本。 Instead of that it outputs the input of those variables but i want it to literally read "$1" and "$2". 取而代之的是,它输出这些变量的输入,但我希望它从字面上读取“ $ 1”和“ $ 2”。 Help? 救命?

Escape it: 逃脱它:

echo "ls /SomeFolder | grep \$1 | xargs cat | grep something | grep  .txt | awk '{print \$2}' | sed 's/;\$//';" >> script2.sh

Quote it: 引用它:

echo "ls /SomeFolder | grep "'$'"1 | xargs cat | grep something | grep  .txt | awk '{print "'$'"2}' | sed 's/;"'$'"//';" >> script2.sh

or like this: 或像这样:

echo 'ls /SomeFolder | grep $1 | xargs cat | grep something | grep  .txt | awk '\''{print $2}'\'' | sed '\''s/;$//'\'';' >> script2.sh

Use quoted here document : 使用此处引用的文档

cat << 'EOF' >> script2.sh
ls /SomeFolder | grep $1 | xargs cat | grep something | grep  .txt | awk '{print $2}' | sed 's/;$//';
EOF

Basically you want to prevent expansion, ie. 基本上,您想防止扩展,即。 take the string literaly. 取字面意义上的字符串。 You may want to read bashfaq quotes 您可能需要阅读bashfaq报价

First, you'd never write this (see https://mywiki.wooledge.org/ParsingLs , http://porkmail.org/era/unix/award.html and you don't need greps+seds+pipes when you're using awk): 首先,你永远也不会写这篇文章(见https://mywiki.wooledge.org/ParsingLshttp://porkmail.org/era/unix/award.html你不需要里grep + SEDS +管道,当你正在使用awk):

ls /SomeFolder | grep $1 | xargs cat | grep something | grep  .txt | awk '{print $2}' | sed 's/;$//'`

you'd write this instead: 您可以这样写:

find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -exec \
    awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}' {} +

or if you prefer using print | xargs 或者如果您喜欢使用print | xargs print | xargs instead of -exec : print | xargs而不是-exec

find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -print0 |
    xargs -0 awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}'

and now to append that script to a file would be: 现在将该脚本附加到文件将是:

cat <<'EOF' >> script2.sh
find /SomeFolder -mindepth 1 -maxdepth 1 -type f -name "*$1*" -print0 |
    xargs -0 awk '/something/ && /.txt/{sub(/;$/,"",$2); print $2}'
EOF

Btw, if you want the . 顺便说一句,如果你想要的. in .txt to be treated literally instead of as a regexp metachar meaning "any character" then you should be using \\.txt instead of .txt . .txt中将其按字面意义处理,而不是被视为表示“任何字符”的正则表达式元字符,那么您应该使用\\.txt而不是.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望我的shell脚本生成一个包含输出的文件 - i want my shell script to generate a file with the output in it Bash脚本,当我不希望它回显时 - Bash script, echo is wrapping when I don't want it to 为什么在我关闭文件描述符并想要回显一些文本后,我有错误的文件描述符? - Why do I have and error bad file descriptor after I close my file descriptor and want to echo some text? 在脚本中创建带有回显的文件时出现错误 - Getting error when creating a file with echo in my script 如何验证我的 bash 脚本以显示在提示用户时只能输入 2 个文件名或使用 echo 语句出错 - How do I validate my bash script to show that only 2 file names can be typed when user is prompted or errors out with echo statement bats - 如何在bats脚本中回显文件名以进行报告 - bats - how can i echo the file name in a bats script for reporting 我正在尝试将期望脚本回显到文件中,但没有成功 - I am trying to echo an expect script into a file but no success 我想使用parallel-ssh在多个服务器上运行bash脚本,但它只是打印echo语句 - I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements echo $ PATH从字面上显示“ $ PATH”作为输出 - echo $PATH literally prints “$PATH” as the output Bash脚本文件描述符echo - Bash Script File Descriptor echo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM