How-to enforce check on getopts command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How-to enforce check on getopts command
# 15  
Old 12-08-2016
Tools

Quote:
Originally Posted by RudiC
Why STRANGELY? It performs exactly as specified and expected. man bash:


How about
Code:
callme $*

?
callme $* resolved the issue.

Frankly i never knew this .... hardly worked with functions .... Thank you All
# 16  
Old 12-09-2016
If you might ever have cases where some of your script's arguments contain IFS characters, you'll need to use:
Code:
callme "$@"

instead of:
Code:
callme $*

And, note that processing arguments in a function (using local variables) makes those variables invisible to the calling script. If you intend to invoke callme and then use $file_in in your main script after callme returns; it won't work. (1st because your function exits instead of returning and 2nd because you're using local variables that do not exist outside the function.)

One might also wonder why you would have a utility that has one, and only one, option and that option is not optional, but is instead required to be present. Why not just treat it as an operand instead of an option? The then processing is much simpler:
Code:
#!/bin/bash
IAm=${0##*/}
if [ $# -eq 1 ]
then	file_in="$1"
else	printf 'Usage: %s filename\n' "$IAm" >&2
	exit 1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to enforce interactive keytool?

i am reading line by line from a file as below while IFS= read -r var do ... ... ... done < "hello.txt" I added the keytool command in the do while loop as below. while IFS= read -r var do ... keytool -genkey -alias $fname -keyalg RSA -keystore $fname.jks -keysize 2048 ... done... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies

3. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

4. Shell Programming and Scripting

using getopts to parse a command line

i have the following scenario want to run the following script with manadory and optional argumnets Manadory options are : filename="" port="" optional arguments type -t balances -b bal prices -p ./test filename port -t A -b bal my code i have that won't parse the options is... (1 Reply)
Discussion started by: nano2
1 Replies

5. Shell Programming and Scripting

Help with getopts command

Hello All, I have shell script as below in a.ksh. #! /usr/bin/ksh while getopts a: b: ab:f: VAR do case $VAR in a) A=${OPTARG} echo $A;; b) B=${OPTARG} echo $B;; ab) AB=${OPTARG} echo $AB ;; f) F=${OPTARG} echo $F ;; esac done When I execute sh a.ksh -a 1 -b 2 -ab 3 -f 4 as below... (7 Replies)
Discussion started by: tonsat
7 Replies

6. UNIX for Dummies Questions & Answers

getopts - command line arguments

Hi, I'm having problems with a script where I wanted every single option specified in the command line to have an argument taken with it, but for some reason only d works in the code I will be showing below. For example if I did ./thisfile -a something it would come up with "a chosen with " as... (2 Replies)
Discussion started by: IceX
2 Replies

7. Solaris

How to enforce all users to change their password

Hi All, How to enforce all users to change their password when they try to login. I am having Solaris 9 and 10. Even it would be much better if anyone can say to enforce all users to change their password next morning they login. Thanks in advance, Deepak (3 Replies)
Discussion started by: naw_deepak
3 Replies

8. Solaris

How to enforce login as specific user in Solaris

Hi, I need to implement something that will enforce login to a Solaris server as a particular, specifed user. After this login stage, users will be able to "su -" to whichever user they wish, by which time their activity will be captured by some sort of script (yet to be written). What I need... (7 Replies)
Discussion started by: jamiegeo1
7 Replies

9. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

10. HP-UX

How to enforce users not to modify their command history.

As a system administrator. sometimes we see the users are trying some commands dangerous for the system health and remove them from their individual coomand history file. How it is possible to enforce that the normal usres will will not be able to modify the history. Thanks in advance. Partha (4 Replies)
Discussion started by: partha_bhunia
4 Replies
Login or Register to Ask a Question