getopts usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts usage
# 1  
Old 06-19-2012
getopts usage

Hi, I have a ksh, where in im using getopts. below is the sample script

Code:
 
#!/bin/ksh
set -x
while getopts j:s:b:e: opt; do
case $opt in
j) var3=${OPTARG};;
s) var1=y;;
b) var2=y;;
e) var4=`cat ${OPTARG}`;;
*) print "Unrecognized parameter ($opt).\n"
exit 1;;
esac
done
shift $OPTIND-1
if (( $# < 2 )); then
exit 1
fi
file_name=$1
dir=$2
var1=${var1:-n}
var2=${var2:-n}
var3=${var3:-'##nil##'}
var4=${var4:-`whoami`@abc.com}
echo $file_name
echo $dir
echo $var1
echo $var2
echo $var3
echo $var4

when i execute the script with the below command along with all options, except for var2 everything is coming
as expected. For var2 i'm expecting a value 'y' but var2 is getting the default value 'n'

Code:
 
> test_getopts -j job1 -s -b -e mail.txt file_name1 dir1
+ getopts j:s:b:e: opt
+ var3=job1
+ getopts j:s:b:e: opt
+ var1=y
+ getopts j:s:b:e: opt
+ + cat mail.txt
var4=name@mail.com
+ getopts j:s:b:e: opt
+ shift 7-1
+ (( 2 < 2 ))
+ file_name=file_name1
+ dir=dir1
+ var1=y
+ var2=n
+ var3=job1
+ var4=name@mail.com
+ echo file_name1
file_name1
+ echo dir1
dir1
+ echo y
y
+ echo n
n
+ echo job1
job1
+ echo name@mail.com
name@mail.com

is the getopts used in the right way .. can someone explain what is happening...

thanks
# 2  
Old 06-19-2012
AFAICT, s and b do not need extra arguments, do they?
I would replace this command:
while getopts j:s:b:e: opt; do to while getopts j:sbe: opt; do

Also, try this, for debugging purpose:
s) var1=y; echo "$OPTARG";;

Other than that, I don't see anything wrong...
This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 06-19-2012
yes thats working ...thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

2. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies

3. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

4. Shell Programming and Scripting

Confused with the usage of one variable usage

Hi All I am not able to understand the usage of d# in the below variable declaration. FILE_LOC contains the directory path And also help me to know about what will be saved in the variable j. Thanks!!! j=${d#${FILE_LOC}/} (2 Replies)
Discussion started by: mohanm
2 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Programming

CPU usage and memory usage

Please tell me solaris functions/api for getting following information 1- Function that tells how much memory used by current process 2- Function that tells how much memory used by all running processes 3- Function that tells how much CPU is used by current process 4- Function that tells how... (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

9. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

10. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies
Login or Register to Ask a Question