【问题标题】:Make a file accept only two arguements from the command-line使文件只接受来自命令行的两个参数
【发布时间】:2015-04-20 06:25:43
【问题描述】:

您好,我在理解一个方面遇到了很多麻烦,我应该做什么以及两个我应该如何实现这一点。首先,我将逐步说明我的程序应该执行的过程。

1) 脚本在 Korn shell 中执行。

2) 将shell脚本文件命名为asg6s。

3) asg6s 文件是可执行文件,并且只接受来自命令行的两个参数。

4) asg6s 文件接受任意两个命名文件作为文本文件的两个参数。

5) 创建一个文件,其中包含“Message Holder”作为文件中的文本。该文件作为第一个参数在命令行中输入。

6) 第二个参数是一个文件,无论您选择什么名称 接受来自 asg6s 文件的输出。

7) 命令行必须具有以下内容:asg6s [输入文件名] [输出文件名] 才能进行任何处理。输入文件必须包含上面 5) 中详述的单词。输出文件是在命令行执行时创建的,而不是之前。

8) 脚本首先检查命令行上是否有两个参数。如果命令行没有两个参数,则显示一条错误消息,其中包含命令行上的模式建议并退出脚本。

9) 如果满足 8) 中的条件,则脚本必须独立检查由第一个参数命名的文件是否存在并包含一些文本。如果命令行的第一个参数不是存在且包含一些文本的文件,则显示包含文件名的错误消息并退出脚本。

好的,就我所得到的代码而言,以下是:

#!/bin/ksh
#Reroute to directory of used files
cd /class
#Make the asg6s accept only two arguements
if [[ $# -ne 2 ]];then
    print "Does not equal two arguments"
    exit
fi
#Creates file containing Message Holder
cat message
Message Holder
#File that accepts the output of file message
cp message message1
#Determine if first argument is empty or null
if [ -z "$1"]
    echo "File does not exist"
fi

显然我错过了一些东西。我认为主要是我没有将文件设置为只接受来自命令行的两个参数。任何帮助,将不胜感激。即使它是帮助我理解我不理解的东西的链接。谢谢

【问题讨论】:

    标签: unix command-line-arguments ksh


    【解决方案1】:

    你很接近。当我阅读您的脚本时,我了解到您在 /class/cm325d/06/dASG6 文件夹中有一个文件 message,其内容为 Message Holder。使用 cat message 并显示输出不属于您的脚本。您正试图将其用作您的论点 1.
    我将使用 ## 来解释我所做的更改或当您想要改进脚本时可以做什么。
    我很高兴您查看并调试了脚本,不高兴我一次没有正确编写它。 改进后的脚本(参见 # EDIT 行):

        #!/bin/ksh
        ## function usage can be called more times, so you write the code on 1 place
        ## I like to add exit in the usage function.
        ## Not correct, I should rename the function to usage_and_exit
    
    # EDIT not "function usage {" but "usage() {"
    ## There are two ways to declare a function with litthe differences.
    ## One difference you see here, the value of $0 can be either the functionname 
    ## or the scriptname.
        usage() {
           ## $0 is replaced by the name of the inputfile
           echo Usage $0 inputfile outputfile 
           ## Escaping double qoutes for showing them
           echo "The inputfile must have the text \"${must_have_text}\""
           ## Use exit 0 when all is OK and exit 1 (or higher) when you have an error
           exit 1
        }
    
    # EDIT Moved this line to be the first lain of the main code.
    ## Your function usage() must be able to  use the variable!
        must_have_text="Message Holder"
    
        # Reroute to directory of used files
        cd /class/
        ## When the path is incorrect or you have incorrect rights you get problems.
        ## Checking $? or using || is possible here.
    
        #Make the asg6s accept only two arguments
        ## I removed the double [ ] brackets, for the -ne check one is enough.
        if [ $# -ne 2 ]; then
           ## ksh is losing from bash. print is only supported in ksh
           ## I prefer using echo
           echo "Does not equal two arguments"
           ## call function usage above, which will also exit
           usage
        fi
        ## Removed the cat message
        ## Introduced a variable with the mandatory test
    
        #File that accepts the output of file message
        ## Removed line cp message message1, 
    
        #Determine if first argument is empty or null
        ## Not needed, syntax checking $1 was missing "; then".
        ## When $1 is filled, you want to see if it is a file with -f.
        ## First introduce new variables
        infile=$1
    # EDIT outfile should be arg2 not arg1
        outfile=$2
    # EDIT Beteween " and ] always a space (stupid keyboard)
        if [ ! -f "${infile}" ]; then
           echo "File ${infile} does not exist"
           ## exit using function again
           usage
        fi
        ## use $() and not `` for executing unix command inside a command line
        if [ $(grep -c "${must_have_text}" ${infile}) -eq 0 ]; then
           echo "${infile} must have 1 or more times the text ${must_have_text}"
           usage
        fi
        cp ${infile} ${outfile}
    

    【讨论】:

    • 试过了,我不断收到 ./asg6s: line 3: syntax error at line 6: `echo' 意外。尝试添加引号,但它不起作用
    • 好吧,似乎它开始工作得更好我得到:不等于两个参数用法./asg6s inputfile outputfile inputfile必须有文本“Message Holder”
    • 您是否像用法所暗示的那样使用两个参数调用它?
    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2020-11-02
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多