Linux scripting: Passing parameters

有些话、适合烂在心里 提交于 2019-12-13 05:41:28

问题


I'm new to scripting and need a lot of help understanding how to load in a text file that will be passed in by a parameter. I'm afraid that you'll have to really dumb down your answers. Please explain like I am a 10 year old!

For example, how does one write a script that accepts two parameters, a file path and an integer.

Also how do you run the script? would you just call script.sh -f data.txt


回答1:


Could you explain your requirement a bit in detail?

I assume you are looking to extract the Nth column from a given file. You don't need to parse command-line parameters within AWK, instead just pass them from sh(1) itself.

# foo.sh
awk "{ print $"$1" }" $2

Here I'm escaping out of AWK to get the first argument which is the position number. If you find that too confusing you can manipulate ARGC, ARGV. Read awk(1).

Hope that helps.




回答2:


You can do this by manipulating ARGC and ARGV to extract your integer from the argument list in the BEGIN block. You can put your script in a text file with the first line being #!/usr/bin/awk -f and then make the file executable with chmod a+x.




回答3:


This page of the GNU AWK (gawk) manual describes how to process options as getopt would.

Include the function shown on that page in your script.



来源:https://stackoverflow.com/questions/10374706/linux-scripting-passing-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!