简体   繁体   English

如何将参数传递到 shell 脚本中的文件中?

[英]How to pass an argument into a file in shell script?

Is it possible to pass an argument from a shell script and pass it to a file.是否可以从 shell 脚本传递参数并将其传递给文件。 This is what I am essentially trying to achieve:这就是我本质上想要实现的目标:

My script:我的脚本:

#!/bin/bash -

input=$1
DIR=/home/user/

File=$DIR/test.txt

`cat $File` < $input


this is the test.txt:这是 test.txt:

select * from abc where date=$input;

I am pretty new to this stuff.我对这些东西很陌生。 please help if there's a correct approach to it.如果有正确的方法,请提供帮助。

You don't need to use cat but rather echo for this particular scenario.对于这种特定情况,您不需要使用 cat 而是使用 echo 。

echo "select * from abc where date='$input'" > test.txt

The double quotes will redirect and WRITE the echo along with the sentence expanded input variable to the text.txt variable.双引号将重定向并将回声连同句子扩展输入变量一起写入 text.txt 变量。

If you want to append as opposed to write, use:如果要 append 而不是写,请使用:

echo "select * from abc where date='$input'" >> test.txt

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM