Script doesn't print error.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script doesn't print error.
# 1  
Old 05-04-2015
Script doesn't print error.

Hi Gurus,

I have below sample script. I expect it print error when running script without input parameter. but the it doesn't.

would you please help me about this issue.

thanks in advance.

Code:
/script$cat test.ksh
#!/bin/ksh
while getopts :f: arg
do
        case $arg in
                f) echo file is : $OPTARG
                        echo $OPTARG;;
                \?) echo "error";;
        esac
done

Code:
/script$./test.ksh -f abc
file is : abc
abc

Code:
/script$./test.ksh
/script$

# 2  
Old 05-05-2015
If you run the script without options, then it will not enter the while loop.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-05-2015
Hello Ken6503,

Could you please try following script as follows, hope it may help you.
Code:
#!/bin/ksh
while getopts ":f:" arg
do
        case $arg in
                f) echo file is : $OPTARG
                        echo $OPTARG;;
                \?) echo "error";;
                :) echo "Option -$OPTARG requires an argument."
                exit 1
        esac
done

if [[ $OPTIND == 1 ]]
then
        echo "No options were passed"
fi

:) echo "Option -$OPTARG requires an argument."
exit 1

Above code will check if a option let's say -f is given while running script but no option was passed it will catch it.

Now come on to the OPTIND, if we read the man page then we will get to know that OPTIND
Quote:
is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND automatically; it must be manually reset.
So I am checking here if it's value is 1 then it means no argument was provided while running script.

Script run without argument:
Code:
./test_try.ksh
No options were passed

Script run with option and argument:
Code:
./test_try.ksh -f chumma
file is : chumma

chumma

Script run with option without argument:
Code:
./test_try.ksh -f
Option -f requires an argument.


Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 05-05-2015
Maybe something like this comes closer to what you're trying to do:
Code:
#!/bin/ksh
IAm=${0##*/}
file=
while getopts :f: arg
do
        case $arg in
	(f)	file="$OPTARG"
		printf 'Found -f option; setting file="%s"\n' "$file";;
	(\?)	printf 'Usage: %s -f file operand...\n' "$IAm" >&2
		exit 1;;
        esac
done
shift $((OPTIND - 1))
if [ "$file" = "" ]
then	printf '%s: No "-f file" option found.\n' "$IAm" >&2
	exit 2
else	printf 'Last "-f file" option set file to "%s"\n' "$file"
fi
printf 'Operands remaining after option parsing:\n'
printf '\t"%s"\n' "$@"

Note, however, that it is more common to use an operand (rather than an option) to specify a mandatory utility argument.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-05-2015
Thanks everyone. you guys are great people.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

DBMS_OUTPUT.PUT_LINE doesn't print values in shell script

Hello, I'm trying to print the value of my cursor in the dbms_output.put_line in shell script, but it only shows "PL/SQL procedure successfully completed." nothing else. I have set serveroutput on, Below is my script : Any advise would be really helpful. sqlplus -s $ORACLE_LOGON <<EOF >>... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

2. Shell Programming and Scripting

Read in script doesn't work

I am trying to run a script to make a simple modification to a number of similar files. The sed works, but after it runs and the differences are displayed, the script does not read ans to start a renaming script if the user answered Y or y.for i in "$@" do sed -f myfile.sed $i >$i.new diff... (2 Replies)
Discussion started by: wbport
2 Replies

3. Shell Programming and Scripting

If first character doesn't equal '#' and same line contains 'temp', print everything else

(2 Replies)
Discussion started by: snoman1
2 Replies

4. Shell Programming and Scripting

Script works but doesn't?

Hi everyone I'm new here so and I'm just starting to learn a bit of Solaris and I'm working on repairing 10 year old scripts for our system here at work. When I execute the commands at my prompt everything go's through smooth. I'm using gedit to edit my code because I'm still getting used to the... (4 Replies)
Discussion started by: 82280zx
4 Replies

5. Shell Programming and Scripting

my script doesn't work :(

i have this script and when i ejecute it, the console tell me this " sintax error line 41 unexpected element "}" " is the sintaxis ok? #!/bin/bash if ;then { exit 0; } if ; then { sudo /etc/init.d/apache2 start; sudo /etc/init.d/mysql start; php5 & nautilus... (3 Replies)
Discussion started by: keiserx
3 Replies

6. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

7. Shell Programming and Scripting

Help with script.. it Just doesn't work

Hello,, Im verry new to scripting and have some problems with this script i made.. What it does: It checks a directory for a new directory and then issues a couple of commands. checks sfv - not doing right now checks rar - it checks if theres a rar file and when there is it skips to... (1 Reply)
Discussion started by: atmosroll
1 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't run

Hi everyone, I've written a script, I gave it permission by > chmod u+x myscript and then tried to run it with > myscript and it didn't work, so I've written > ./myscript again, it didn't work, so I've written > set path = (. $path) but it didn't work as well. I have some... (6 Replies)
Discussion started by: shira
6 Replies

9. UNIX for Dummies Questions & Answers

cout doesn't print everything

Hi all, I implemented a C++ program and successfully compiled and ran on my laptop. However when I copy my code to another machine (school's sun machine), it didn't run properly. I can compile and run, but cout does not print everything. I used cout in a loop where it iterates no more than 20... (5 Replies)
Discussion started by: SaTYR
5 Replies

10. UNIX for Dummies Questions & Answers

Script that doesn't stop

Hi all, I got a script that runs automatically using the cron file. The script starts running at midnight and suppose to delete image files from folders and sub-folders. The script ends when he finishes deleting or after 5 hours. My problem is that the script doesn't stop running even after 5... (3 Replies)
Discussion started by: biot
3 Replies
Login or Register to Ask a Question