修改 BASH 脚本以处理我告诉它的任何文件
我编写了一个 bash 脚本来运行一系列命令,最终生成一个名为 DataAudit.txt
的文件。如果我正在使用的文件恰好名为 file.csv
,它会很好用。
我对这一切都很陌生,不知道如何编写脚本,以便它可以在我想要审核的任何文件上运行。
该脚本名为 audit.sh
,位于名为 PurgatoryCSV
的文件夹中,我的想法是,我将一个文件放入其中,运行该脚本,然后将该文件移动到我工作流程的下一步。
如果我能在解决这个障碍时获得任何帮助,我将不胜感激。
这是脚本:
#!/bin/bash
echo -n "DATA AUDIT
------------
COLUMN NAMES
------------
" > DataAudit.txt
csvcut -n file.csv >> DataAudit.txt
echo -n "
---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS
---------------------------------------
" >> DataAudit.txt
csvcut -c 1,2,3,4,5 file.csv | head -n 10 >> DataAudit.txt
echo -n "
------------
COLUMN STATS
------------
" >> DataAudit.txt
csvcut file.csv | csvstat >> DataAudit.txt
echo -n "
---END AUDIT" >> DataAudit.txt
I've written a bash script to run a series of commands, culminating in a file called DataAudit.txt
. It works great... if the file I am working with happens to be called file.csv
.
I'm very new to all of this and not sure how to write the script so it can work on whichever file I want to audit.
The script, called audit.sh
, lives in a folder called PurgatoryCSV
and the idea is that I would drop a file in there, run the script, and move the file to the next step in my workflow.
I would be grateful for any help I could get with this roadblock.
Here is the script:
#!/bin/bash
echo -n "DATA AUDIT
------------
COLUMN NAMES
------------
" > DataAudit.txt
csvcut -n file.csv >> DataAudit.txt
echo -n "
---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS
---------------------------------------
" >> DataAudit.txt
csvcut -c 1,2,3,4,5 file.csv | head -n 10 >> DataAudit.txt
echo -n "
------------
COLUMN STATS
------------
" >> DataAudit.txt
csvcut file.csv | csvstat >> DataAudit.txt
echo -n "
---END AUDIT" >> DataAudit.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用从命令行传入的变量:第一个为
$1
,第二个为$2
,依此类推。看起来这里有两个变量,< code>file.csv 和DataAudit.txt
如果将
file.csv
替换为$1
和DataAudit.txt< /code> 与
$2
,您可以现在执行您的脚本:或者,为了提高可读性,通常将它们分配给脚本顶部的命名变量:
然后,在您的代码中,您可以使用
$INPUTFILE
和$OUTPUTFILE
You can use variables that are passed in from the command line:
$1
for the first,$2
for the second, etc. It looks like you have two variables here, thefile.csv
andDataAudit.txt
If you replace
file.csv
with$1
andDataAudit.txt
with$2
, you can now execute your script by doing:Alternatively for more readability, it is common to assign these into named variables at the top of your script:
Then, in your code you can reference these with
$INPUTFILE
and$OUTPUTFILE
尽管这不是问题的一部分,但在这些情况下,此处文档提供了上述脚本的优雅且更清晰的实现,消除了容易出错的重复:
Although it's not part of the question, a here-document in these circumstances provides an elegant and clearer implementation of the above script, removing error prone repetition:
像这样
调用脚本,即
您
必须对传递的文件名进行更多验证。
Something like this
Call the script as
i.e.
You would have to do more validations about the filenames being passed.