如何使一个shell脚本走在命令行中传递参数? [英] How to make a shell script take an argument passed in the command line?
问题描述
我有一个包含以下内容的shell脚本(count_reviews.sh):
I have a shell script (count_reviews.sh) that contains the following:
#!/bin/bash
grep -c "Author" "#1"
我一直在使用$ 1,因为我发现,这会将第一个参数,在命令行,但我仍然得到以下提到的错误尝试。我也用使用chmod + x可让我的文件可执行。
I have tried using "$1" as I found that this takes the first argument in the command line but I still get the error mentioned below. I have also used chmod +x to make my file executable.
脚本计数作者出现在文件中的次数。我需要能够使命令行采取的%./count_reviews.sh hotel_72572.dat
,其中 hotel_72572.dat <输入/ code>是一个示例文件名。倍作者出现的数目将被下方打印出来。当我这样做不过,我得到一个错误
-bash:FG:%:没有这样的工作
。是什么原因造成这一点,我该如何解决?我的 count_reviews.sh
文件在同一目录下所有的我的酒店数据文件,如果该事项。
The script counts the number of times "Author" appears in the file. I am required to be able to make the command line take an input of "% ./count_reviews.sh hotel_72572.dat"
where hotel_72572.dat
is an example file name. The number of times author appears will then be printed out underneath. When I do this however, I am getting an error -bash: fg: %: no such job
. What is causing this and how do I fix it? My count_reviews.sh
file is in the same directory as all of my hotel data files if that matters.
推荐答案
您确实应该使用 $ 1
,但是当您尝试运行脚本只需使用 ./ your_script.sh your_argument
不要在开头添加%
。
You should indeed use $1
, but when you try to run your script just use ./your_script.sh your_argument
don't add the %
in the beginning.
这篇关于如何使一个shell脚本走在命令行中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!