2

I have done very little programming in C++, and I am not really understanding how to use code in KornShell (ksh) on the Knoppix OS. The resources given to us by the professor has been little, so it is hard to work out. The two text books we have for class talks mostly about commands in variations of UNIX, though little to do with ksh.

Write a shell script that accepts exactly one argument which must be a positive integer. (My second one has two)

Ksh Code:

NUMBER=$1 read -p NUMBER # Test that one argument was input. if [[ $# -ne 1 ]];then echo "Please enter an integer as an argument" exit 1 elif [[ $NUMBER -le 0 ]];then # Test value of argument is less than or equal to zero echo "Please enter a number > 0" exit 1 fi while [["$NUMBER" -ne 1]];do printf $NUMBER if [[$NUMBER -gt 1]];then printf "," fi NUMBER=$(($NUMBER-1)) done printf $NUMBER 

When I run this from the shell I keep getting "Please enter an integer as an argument" as an output, though the entry was 3, or something like that.

I noticed that there was not anything for user input, so I tried to enter this myself with

read -p NUMBER 

before the if statement.

What am I missing from the code to continue on with the rest of the script to be run?

1
  • 2
    I've rolled back this question. You can say thanks by upvoting the answers and comments that helped you and accept the one that helped you most. Commented Jan 23, 2014 at 8:27

4 Answers 4

6

You should double quote your variables in [ ... ] conditions, or use the extended [[ ... ]] conditions (yes, a third type of condition). For numeric operations, (( .. )) are usually used (oh my, fourth type!).

See man bash or Advanced Bash Scripting Guide.

Sign up to request clarification or add additional context in comments.

4 Comments

@user3193284: Try asking the professor. When I was teaching, I encouraged my students to ask questions.
The OP script is not at all using csh syntax. elif is definitely supported by bash, ksh and any POSIX compliant shell and is not a csh keyword. Another comment, the question is about ksh but you suggest bash documentation. Despite being both mostly POSIX shell and sharing a lot of extensions, each one have specificities so the bash scripting guide might confuse the OP.
@jlliagre: You are correct about elif. Nevertheless, I had responded to the original question, that had been heavily edited and retagged. In the beginning, it was tagged bash and contained the work in the title.
Ok removing my -1 then. You should remove your first sentence though.
2

The first passed argument should be acquired by using

NUMBER=$1 

don't know what you mean by $X..

Note: $0 is the command executed, for example in ./myscript.sh foo

the $0 = ./myscript.sh and $1 = foo

After you set it to you variable NUMBER you can check if it's a positive integer:

if [[ $NUMBER =~ ^[0-9]+$ ]]; then echo "It's ok!" else echo "BAD :(" fi 

2 Comments

NUMBER = $1 is a syntax error in shell - get rid of the spaces around the =. You can do that in C or C++ or Python or Perl or ..., but not in bash/ksh/sh (don't remember if that's acceptable in csh, but that's ok, really).
Thanks. I found out, with help, that it is very picky on what it accepts as syntax. Must be a space after [[ and before ]].
0

To accept just one argument you should check the number of arguments passed to the script (which is what I think your teacher is asking):

if [[ $# -ne 1 ]] 

$# gives you the number of arguments passed to your script, so it is easy to check the number of argumets that are being passed to it. Now you only need to check if it is an integer and the sign. I don't know what exactly is what your teacher want you to know, but you can use awk's functions to check wether the parameter is a number, an integer and its sign (the sign is also easy with ksh).

2 Comments

I guess not, if your teacher asked for a script that accept exactly 1 parameter $# is the answer. You only need to test $1 once you have checked that only one parameter is passed to the script.
I mean, remove the "" and add a space after [[ and another space before ]] ----> while [[ $NUMBER -ne 1 ]];do
-1

You should remove the read -p NUMBER instruction which defeat the previous one.

You shoud then fix that line:

elif [[ $NUMBER -le 0 ]];then 

to

elif [ "$NUMBER" -le 0 ];then 

The last part is using the wrong tests and is missing an ending new line:

while [["$NUMBER" -ne 1]];do printf $NUMBER if [[$NUMBER -gt 1]];then printf "," fi NUMBER=$(($NUMBER-1)) done printf $NUMBER 

should be

while [ $NUMBER -ne 1 ];do printf $NUMBER if [ $NUMBER -gt 1 ];then printf "," fi NUMBER=$((NUMBER-1)) done printf "%s\n" $NUMBER 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.