修改 BASH 脚本以处理我告诉它的任何文件

发布于 2024-12-13 00:49:56 字数 883 浏览 1 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2024-12-20 00:49:56

您可以使用从命令行传入的变量:第一个为 $1,第二个为 $2,依此类推。看起来这里有两个变量,< code>file.csv 和 DataAudit.txt

如果将 file.csv 替换为 $1DataAudit.txt< /code> 与 $2 ,您可以现在执行您的脚本:

./audit.sh myotherfile.csv MyOtherAudit.txt

或者,为了提高可读性,通常将它们分配给脚本顶部的命名变量:

INPUTFILE=$1
OUTPUTFILE=$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, the file.csv and DataAudit.txt

If you replace file.csv with $1 and DataAudit.txt with $2, you can now execute your script by doing:

./audit.sh myotherfile.csv MyOtherAudit.txt

Alternatively for more readability, it is common to assign these into named variables at the top of your script:

INPUTFILE=$1
OUTPUTFILE=$2

Then, in your code you can reference these with $INPUTFILE and $OUTPUTFILE

乖乖公主 2024-12-20 00:49:56

尽管这不是问题的一部分,但在这些情况下,此处文档提供了上述脚本的优雅且更清晰的实现,消除了容易出错的重复:

#!/bin/bash

usage () { echo "${0##*/} inputfile outputfile"; exit 1; }

(($#==2)) || usage

INPUTFILE="$1"
OUTPUTFILE="$2"

cat <<EOF >$OUTPUTFILE # all that follows upto 'EOF' will go to the outputfile
DATA AUDIT

------------
COLUMN NAMES
------------

$(csvcut -n $INPUTFILE)

---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS 
---------------------------------------

$(csvcut -c 1,2,3,4,5 $INPUTFILE | head -n 10)

------------
COLUMN STATS
------------

$(csvcut $INPUTFILE | csvstat )

---END AUDIT
EOF

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:

#!/bin/bash

usage () { echo "${0##*/} inputfile outputfile"; exit 1; }

(($#==2)) || usage

INPUTFILE="$1"
OUTPUTFILE="$2"

cat <<EOF >$OUTPUTFILE # all that follows upto 'EOF' will go to the outputfile
DATA AUDIT

------------
COLUMN NAMES
------------

$(csvcut -n $INPUTFILE)

---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS 
---------------------------------------

$(csvcut -c 1,2,3,4,5 $INPUTFILE | head -n 10)

------------
COLUMN STATS
------------

$(csvcut $INPUTFILE | csvstat )

---END AUDIT
EOF
維他命╮ 2024-12-20 00:49:56

像这样

#!/bin/bash

if [ $# -ne 2 ]
then
  echo "Usage: `basename $0` {inputFile} {outputFile}"
  exit 1
fi

InputFile="$1"
OutputFile="$2"

echo -n "DATA AUDIT

------------
COLUMN NAMES
------------

" > "$OutputFile"
csvcut -n "$InputFile" >> "$OutputFile"
echo -n "

---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS 
---------------------------------------

" >> "$OutputFile"
csvcut -c 1,2,3,4,5 "$InputFile" | head -n 10 >> "$OutputFile"
echo -n "

------------
COLUMN STATS
------------

" >> "$OutputFile"
csvcut "$InputFile" | csvstat >> "$OutputFile"
echo -n "

---END AUDIT" >> "$OutputFile"

调用脚本,即

audit.sh InputFile OutputFile

audit.sh file.csv DataAudit.txt

必须对传递的文件名进行更多验证。

Something like this

#!/bin/bash

if [ $# -ne 2 ]
then
  echo "Usage: `basename $0` {inputFile} {outputFile}"
  exit 1
fi

InputFile="$1"
OutputFile="$2"

echo -n "DATA AUDIT

------------
COLUMN NAMES
------------

" > "$OutputFile"
csvcut -n "$InputFile" >> "$OutputFile"
echo -n "

---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS 
---------------------------------------

" >> "$OutputFile"
csvcut -c 1,2,3,4,5 "$InputFile" | head -n 10 >> "$OutputFile"
echo -n "

------------
COLUMN STATS
------------

" >> "$OutputFile"
csvcut "$InputFile" | csvstat >> "$OutputFile"
echo -n "

---END AUDIT" >> "$OutputFile"

Call the script as

audit.sh InputFile OutputFile

i.e.

audit.sh file.csv DataAudit.txt

You would have to do more validations about the filenames being passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文