Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过shell脚本以正确的格式将文件中的参数转换为python脚本_Python_Bash_Argument Passing - Fatal编程技术网

通过shell脚本以正确的格式将文件中的参数转换为python脚本

通过shell脚本以正确的格式将文件中的参数转换为python脚本,python,bash,argument-passing,Python,Bash,Argument Passing,我有以下shell脚本: #! /bin/sh while read page_section page=${page_section%%\ *} section=${page_section#* } #NOTE: `%* }` is NOT a comment wget --quiet --no-proxy www.cs.sun.ac.za/hons/$page -O html.tmp & wait # echo ${page_section%%\ *} #

我有以下shell脚本:

#! /bin/sh

while read page_section
  page=${page_section%%\ *}
  section=${page_section#* }     #NOTE: `%* }` is NOT a comment

  wget --quiet --no-proxy www.cs.sun.ac.za/hons/$page -O html.tmp & wait

#  echo ${page_section%%\ *} # verify correct string chopping
#  echo ${page_section#* }   # verify correct string chopping

  ./DokuWikiHtml2Latex.py html.tmp $section & wait
done < inputfile
该脚本下载大量在
inputfile
中指定的网页,然后必须将行的其余部分(例如“Implementation-1”或“Research\arears-1”)传递给python脚本

现在是棘手的一点。处理此示例文件的第三行时,它将“Research\Areas”作为两个单独的参数传递给python脚本,如下所示:

>>> print sys.argv
['./DokuWikiHtml2Latex.py', 'html.tmp', 'Research', 'Areas', '-1']
如何将输入文件中的多单词部分(如“研究区域”)放入python脚本的单个参数中?我试过逃离“\”,也试过

./DokuWikiHtml2Latex.py html.tmp `echo ${section#* }`
除其他外,但毫无用处


输入行末尾的数字是另一个参数,但它是可选的。

只需让
读取
进行解析即可:

while read page section rest
do
    echo "Page: $page"
    echo "Section: $section"
done < inputfile
阅读页面部分休息时
做
echo“页面:$Page”
echo“部分:$Section”
完成<输入文件
要优雅地处理可选参数,请使用数组:

while read -a fields
do
    wget --quiet --no-proxy "www.cs.sun.ac.za/hons/${fields[0]}" -O html.tmp
    unset "fields[0]"
    ./DokuWikiHtml2Latex.py html.tmp "${fields[@]}"
done < inputfile
读取时-a字段
做
wget--quiet--无代理“www.cs.sun.ac.za/hons/${fields[0]}”-O html.tmp
取消设置“字段[0]”
./DokuWikiHtml2Latex.py html.tmp“${fields[@]}”
完成<输入文件

总是引用你的变量

通常多字参数可以使用引号作为一个参数传递,因此:

doku.php?id=ndewet:description "Research Areas" -1

在$section周围加引号:

./DokuWikiHtml2Latex.py html.tmp "$section" & wait

您应该在取消设置的数组元素周围加上引号,以防止文件全球化:
unset“fields[0]”
(如果存在名为“fields0”的文件)。演示:
test=(1233);触摸测试0;未设置测试[0];p检验;取消设置“测试[0]”;声明-p测试
不客气。我忘了演示名为
test0
的变量(如果存在的话)会因为全局搜索和文件的存在而被取消设置:
test=(1 2 3);test0=4;触摸测试0;未设置测试[0];echo“test0=$test0”;p检验;取消设置“测试[0]”;声明-p测试
./DokuWikiHtml2Latex.py html.tmp "$section" & wait