使文件只接受来自命令行的两个参数
Make a file accept only two arguements from the command-line
你好,我在理解一个方面有很多困难,我应该做什么,第二个我应该如何实现它。首先,我将逐步说明我的程序应该执行的过程。
1) 脚本在Korn中执行shell.
2) 将shell脚本文件命名为asg6s。
3) asg6s 文件是可执行的,并且正好接受来自命令行的两个参数。
4) asg6s 文件接受任意两个命名文件作为文本文件的两个参数。
5) 创建一个文件,其中包含“Message Holder”作为文件中的文本。此文件作为第一个参数在命令行中输入。
6) 第二个参数是一个文件,你可以 select
接受 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 ""]
echo "File does not exist"
fi
显然我遗漏了一些东西。我认为最主要的是我没有将文件设置为只接受来自命令行的两个参数。任何帮助,将不胜感激。即使它是帮助我理解我不理解的链接。谢谢
你很接近。当我阅读您的脚本时,我了解到您在 /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 [=10=] can be either the functionname
## or the scriptname.
usage() {
## [=10=] is replaced by the name of the inputfile
echo Usage [=10=] 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 was missing "; then".
## When is filled, you want to see if it is a file with -f.
## First introduce new variables
infile=
# EDIT outfile should be arg2 not arg1
outfile=
# 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}
你好,我在理解一个方面有很多困难,我应该做什么,第二个我应该如何实现它。首先,我将逐步说明我的程序应该执行的过程。
1) 脚本在Korn中执行shell.
2) 将shell脚本文件命名为asg6s。
3) asg6s 文件是可执行的,并且正好接受来自命令行的两个参数。
4) asg6s 文件接受任意两个命名文件作为文本文件的两个参数。
5) 创建一个文件,其中包含“Message Holder”作为文件中的文本。此文件作为第一个参数在命令行中输入。
6) 第二个参数是一个文件,你可以 select 接受 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 ""]
echo "File does not exist"
fi
显然我遗漏了一些东西。我认为最主要的是我没有将文件设置为只接受来自命令行的两个参数。任何帮助,将不胜感激。即使它是帮助我理解我不理解的链接。谢谢
你很接近。当我阅读您的脚本时,我了解到您在 /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 [=10=] can be either the functionname
## or the scriptname.
usage() {
## [=10=] is replaced by the name of the inputfile
echo Usage [=10=] 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 was missing "; then".
## When is filled, you want to see if it is a file with -f.
## First introduce new variables
infile=
# EDIT outfile should be arg2 not arg1
outfile=
# 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}