Ben*_*ton 24 unix shell ksh pipe
我在ksh中编写了一个shell脚本,将CSV文件转换为Spreadsheet XML文件.它需要一个现有的CSV文件(脚本中的变量路径),然后创建一个新的输出文件.xls.该脚本没有位置参数.CSV的文件名当前已硬编码到脚本中.
我想修改脚本,以便它可以从管道中获取输入的CSV数据,这样.xls输出数据也可以通过管道传输或重定向(>)到命令行上的文件.
这是如何实现的?
我正在努力寻找有关如何编写shell脚本以从管道获取输入的文档.似乎'read'仅用于来自kb的std输入.
谢谢.
编辑:下面的脚本信息(现在修改为通过猫从管道输入,根据问题的答案.
#!/bin/ksh
#Script to convert a .csv data to "Spreadsheet ML" XML format - the XML scheme for Excel 2003
#
# Take CSV data as standard input
# Out XLS data as standard output
#
DATE=`date +%Y%m%d`
#define tmp files
INPUT=tmp.csv
IN_FILE=in_file.csv
#take standard input and save as $INPUT (tmp.csv)
cat > $INPUT
#clean input data and save as $IN_FILE (in_file.csv)
grep '.' $INPUT | sed 's/ *,/,/g' | sed 's/, */,/g' > $IN_FILE
#delete original $INPUT file (tmp.csv)
rm $INPUT
#detect the number of columns and rows in the input file
ROWS=`wc -l < $IN_FILE | sed 's/ //g' `
COLS=`awk -F',' '{print NF; exit}' $IN_FILE`
#echo "Total columns is $COLS"
#echo "Total rows is $ROWS"
#create start of Excel File
echo "<?xml version=\"1.0\"?>
<?mso-application progid=\"Excel.Sheet\"?>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:o=\"urn:schemas-microsoft-com:office:office\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:html=\"http://www.w3.org/TR/REC-html40\">
<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">
<Author>Ben Hamilton</Author>
<LastAuthor>Ben Hamilton</LastAuthor>
<Created>${DATE}</Created>
<Company>MCC</Company>
<Version>10.2625</Version>
</DocumentProperties>
<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">
<WindowHeight>6135</WindowHeight>
<WindowWidth>8445</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>120</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID=\"Default\" ss:Name=\"Normal\">
<Alignment ss:Vertical=\"Bottom\" />
<Borders />
<Font />
<Interior />
<NumberFormat />
<Protection />
</Style>
<Style ss:ID=\"AcadDate\">
<NumberFormat ss:Format=\"Short Date\"/>
</Style>
</Styles>
<Worksheet ss:Name=\"Sheet 1\">
<Table>
<Column ss:AutoFitWidth=\"1\" />"
#for each row in turn, create the XML elements for row/column
r=1
while (( r <= $ROWS ))
do
echo "<Row>\n"
c=1
while (( c <= $COLS ))
do
DATA=`sed -n "${r}p" $IN_FILE | cut -d "," -f $c `
if [[ "${DATA}" == [0-9][0-9]\.[0-9][0-9]\.[0-9][0-9][0-9][0-9] ]]; then
DD=`echo $DATA | cut -d "." -f 1`
MM=`echo $DATA | cut -d "." -f 2`
YYYY=`echo $DATA | cut -d "." -f 3`
echo "<Cell ss:StyleID=\"AcadDate\"><Data ss:Type=\"DateTime\">${YYYY}-${MM}-${DD}T00:00:00.000</Data></Cell>"
else
echo "<Cell><Data ss:Type=\"String\">${DATA}</Data></Cell>"
fi
(( c+=1 ))
done
echo "</Row>"
(( r+=1 ))
done
echo "</Table>\n</Worksheet>\n</Workbook>"
rm $IN_FILE > /dev/null
exit 0
Run Code Online (Sandbox Code Playgroud)
che*_*ner 42
命令从启动它们的进程继承其标准输入.在您的情况下,您的脚本为其运行的每个命令提供标准输入.一个简单的示例脚本:
#!/bin/bash
cat > foo.txt
Run Code Online (Sandbox Code Playgroud)
将数据导入shell脚本会导致cat读取该数据,因为cat从脚本继承了其标准输入.
$ echo "Hello world" | myscript.sh
$ cat foo.txt
Hello world
Run Code Online (Sandbox Code Playgroud)
read如果您没有其他命令来读取或处理脚本的标准输入,则shell将提供该命令,用于将标准输入中的文本读取到shell变量中.
#!/bin/bash
read foo
echo "You entered '$foo'"
$ echo bob | myscript.sh
You entered 'bob'
Run Code Online (Sandbox Code Playgroud)
Phi*_*ese 31
这里有一个问题.如果您在没有首先检查以确保stdin上有输入的情况下运行脚本,那么它将挂起直到输入内容.
因此,为了解决这个问题,您可以检查以确保首先存在stdin,如果没有,则使用命令行参数(如果给定).
创建一个名为"testPipe.sh"的脚本
#!/bin/bash
# Check to see if a pipe exists on stdin.
if [ -p /dev/stdin ]; then
echo "Data was piped to this script!"
# If we want to read the input line by line
while IFS= read line; do
echo "Line: ${line}"
done
# Or if we want to simply grab all the data, we can simply use cat instead
# cat
else
echo "No input was found on stdin, skipping!"
# Checking to ensure a filename was specified and that it exists
if [ -f "$1" ]; then
echo "Filename specified: ${1}"
echo "Doing things now.."
else
echo "No input given!"
fi
fi
Run Code Online (Sandbox Code Playgroud)
然后测试:
让我们在test.txt文件中添加一些东西,然后将输出传递给我们的脚本.
printf "stuff\nmore stuff\n" > test.txt
cat test.txt | ./testPipe.sh
Run Code Online (Sandbox Code Playgroud)
输出:
Data was piped to this script!
Line: stuff
Line: more stuff
现在让我们测试一下,如果没有提供任何输入:
./testPipe.sh
Run Code Online (Sandbox Code Playgroud)
输出:
No input was found on stdin, skipping!
No input given!
现在让我们测试是否提供有效的文件名:
./testPipe.sh test.txt
Run Code Online (Sandbox Code Playgroud)
输出:
No input was found on stdin, skipping!
Filename specified: test.txt
Doing things now..
最后,让我们使用无效的文件名进行测试:
./testPipe.sh invalidFile.txt
Run Code Online (Sandbox Code Playgroud)
输出:
No input was found on stdin, skipping!
No input given!
说明: 如果stdin在shell中可用,则read和cat等程序将使用stdin,否则它们将等待输入.
在他的答案显示如何检查stdin输入时,来自此页面的Mike会转到Mike:https: //unix.stackexchange.com/questions/33049/check-if-pipe-is-empty-and-run-a-command-上的数据,如果-IT-心不是?newreg = fb5b291531dd4100837b12bc1836456f
如果外部程序(您正在编写脚本)已经从stdin中获取输入,则您的脚本不需要执行任何操作。例如,awk从stdin读取,因此提供了一个简短的脚本来计算每行的单词数:
#!/bin/sh
awk '{print NF}'
Run Code Online (Sandbox Code Playgroud)
然后
./myscript.sh <<END
one
one two
one two three
END
Run Code Online (Sandbox Code Playgroud)
输出
1
2
3
Run Code Online (Sandbox Code Playgroud)