handling white spaces with getopt


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers handling white spaces with getopt
# 1  
handling white spaces with getopt

Hi

I'm trying to ensure that I have catered for all situations with my getopt cases.

One other situation I want to cover is should the user enter the script without any preceding arguments eg:

Code:
./script_eg

I need the script to the direct the user to the helpfile

I have tried this:

Code:
while getopts " i: p: o: n: h " option
do
case $option in
i ) id="$OPTARG";;
p ) paramval="$OPTARG";;
o ) oldfile="$OPTARG";;
n ) newfile="$OPTARG";;
h ) displayHelp;exit;;
? ) displayHelp;exit;;



esac;
done

the "?" only directs the user to the helpfile if the user enters anything other than the listed arguments for eg:

Code:
./script_eg -s

since s is not listed as one of my arguments, it directs me to the helpfile.

what will cater for whitespaces or null values?
# 2  
getopt doesn't handle white space, however all is not lost.

just change you script and insert a line checking for arguments or lack thereof:

Code:
[[ $# -eq "0" ]] && displayHelp

while getopts " i: p: o: n: h " option

Note: you would want an exit at end of your displayHelp function, so it exits cleanly after displaying your help message(s).
# 3  
Safest bet is that you don't leave it up to run-time. You define all vars ahead of the getopts section of your code, which your other posts seem to suggest you've done already. getopts is only going to rely on the case..esac statement.

Further safeguards would be to explicitly fall back to your earlier variable value:
Code:
myarg="${1:-myarg}"

Likewise, since the -s switch shown isn't in your case..esac statement, you'd accommodate this by making your last entry as follows:
Code:
while getopts " i: p: o: n: h " option
do
case $option in
i ) id="$OPTARG";;
p ) paramval="$OPTARG";;
o ) oldfile="$OPTARG";;
n ) newfile="$OPTARG";;
h | ? | * ) displayHelp;exit;;

esac;
done

This forces bum switches into the usage statement you've already defined.
# 4  
Thank u so much. This works perfectly
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #502
Difficulty: Medium
If a function uses a particular process or algorithm such as a Fast Fourier Transform to perform an operation, it would not be appropriate to document it in a series of comments in the source code.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - read white spaces

Hello! I have one problem with my bash script - I would like to be able to read white space characters from stdin (for example single " ") - can I acomplish that somehow? I need to read only one character at the time, so I use read -s -n 1 var but it doesn't work for whitespaces apparently. ... (3 Replies)
Discussion started by: xqwzts
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Help with using tr - Removing white spaces

Hi, I have a file that contains whitespaces with spaces and spaces and tabs on each line and am wanting to remove the whitespaces. My version of sed is one that does not recognize \t etc. The sed and awk one-liners below that I found via Google both does not work. So my next best... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

4. Shell Programming and Scripting

white spaces in bash autocompletion

Hello dear community! I've recently written a BASH function for auto completion of options. It works like following: if a user types a command and then an argument to this command which starts with "^-" and then presses TAB, then 'user_command --help (or -h)' is invoked and possible options are... (0 Replies)
Discussion started by: sidorenko
0 Replies

5. Shell Programming and Scripting

ksh: removing all white spaces

'String' file contains the following contents, D11, D31, D92, D29, D24, using ksh, I want to remove all white spaces between characters no matter how long the string is. Would you please give me some help? (1 Reply)
Discussion started by: yoonius
1 Replies

6. UNIX for Dummies Questions & Answers

deleting white spaces in a file

Hello Guys, I am a newbie to unix. I am having a requirement. Please help me for finding a solution for this, I am having a file as mentioned below: $ cat shank ackca acackac akcajc akcjkcja akcj ckcklc I want to delete all the white spaces in this file, I tried... (2 Replies)
Discussion started by: mraghunandanan
2 Replies

7. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

8. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies

Featured Tech Videos