DonCraguns suggestion is a very good one, but you showed - apart from syntactical errors - some lack of consideration. Lets go over your script:
This is the first problem: you expect a user to enter what you think he will enter. Never trust user input - in no way at all. The expression "$c -le 9" is an integer comparison and this in turn depends on only an integer being supplied by the user. How would your script react to this input:
or this:
In the first case your comparison will fail because you try to compare integer to string and in the second case it will even fail at c=$1 because the multi-word into which "$1" expands into will break the line. "$c" wil be assigned "all" (the first word) and the other words will lead to syntax errors.
To avoid the multi-word problem use quoting:
To avoi the integer-string problem first make sure your input is indeed an integer before you use integer comarison. This is called type-checking. Bottom line: always be paranoid about user input and assume it will make the least sense possible. It usually does.
Even if this would work as expected it would again break on multi-word input because of the missing quoting. Further, what would happen with this input:
In fact it would NOT match at all! The reason is that UNIX IS CASE-SENSITIVE! "[a-z]" and "[A-Z]" are different and if you want to make sure it is indeed a character (as opposed to a lowercase character you have to test it for both:
Another thing is a further false assumption: you test (i assume now your tests would do what they are meant to do) for numbers smaller than 10 and single characters and if both tests fail you deduce that it has to be a multi-character string. What about numbers bigger than 10? What about strings like "!", which would fail on both tests before too?
elif [ ${#1} -eq 0 ]
then echo "$(basename $0): argument is an empty string" >&2
exit 2
./file 0
This "0" is taken as a single(1) argument, so the elif statement is skipping
Finally the result shown is:
output :
digit
I don't understand your comment.
If the script I provided is invoked with the single argument 0 then $#(the number of positional parameters) is 1 and ${#1}(the number of characters in the 1st positional parameter) is 1. So the [ ... ] commands in the if and both elif statements evaluate to false so no diagnostic is printed. Therefore the code continues with the case statement to determine what type character was supplied as the value of the single character given as the one and only operand to the script. Since 0 is in the digit character class, the script reports that 0 is a digit.
A way to get the diagnostic "file: argument is an empty string" to print would be to execute the command:
since the number of characters in the empty string (${#1} when $1 is the expansion of "" in the shell after quote removal processing) is 0.
I am trying to pass a second argument like so:
if ] then
export ARG2=$2
else
message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2"
checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument"
fi
however, it always goes... (4 Replies)
Hi,
I have a script that is scheduled with cron and runs every night. The cron part looks like this:
00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE
My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Hello all,
New to C and I'm trying to write a program which can run a unix command. Would like to have the option of giving the user the ability to enter arguments e.g for "ls" be able to run "ls -l".
I would appreciate any help.
Thanks
#include <stdio.h>
#include <unistd.h>
#include... (3 Replies)
Hi all
First of all thanks for everyone to read by doubt.Am beginner in shell scripting
Following are my doubts
i have to pass an argument to shellscript how can i do that
second i have to test the argument and shows error when nothing is passes
third i have to match exact argument... (3 Replies)
Hi all
I have got a file digits.data containing the following data
1 3 4
2 4 9
7 3 1
7 3 10
I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (2 Replies)
I have some folders with this structure:
/data/me/a123xxx
Where "xxx" is some changing series of letters, and "123" changes so folders might look like: /data/me/a123xxx
/data/me/a234ysd
/data/me/a534sds etc. The numbers are what matter to me (they identify the folder), so I created an... (7 Replies)
Hi All,
i have script like below..
echo "1) first option"
echo ""
echo "2) second option"
echo ""
echo "*) please enter the correct option"
read select
case $select in
1) echo "first option selected"
;;
2) echo "second option selected"
;;
*) echo "please enter the correct... (4 Replies)
Hi All,
Calling a function with one argument and storing the return value in a shell script is as below:( so far I know)
value="`fun_1 "argument1"`"
Its working perfectly for me.
Can u help me with passing more than one argument and storing the return value
Thnaks in advance
JS (1 Reply)
Greetings,
I am wrapping the monitoring commands like vmstat, sar, iostat and call via arguments
I want ./unix_stats.sh -v vmstat -p <SEC> -d <Duration>
to give vmstat values, and similarly iostat etc.,.
Also if I give ./unix_stats.sh -v vmstat -i iostat -p <SEC> -d <Duration> should give... (4 Replies)