Sponsored Content
Top Forums UNIX for Dummies Questions & Answers handling white spaces with getopt Post 302411738 by curleb on Friday 9th of April 2010 09:57:06 AM
Old 04-09-2010
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GETOPT(1)						    BSD General Commands Manual 						 GETOPT(1)

NAME
getopt -- parse command options SYNOPSIS
args=`getopt optstring $*` ; errcode=$?; set -- $args DESCRIPTION
The getopt utility is used to break up options in command lines for easy parsing by shell procedures, and to check for legal options. Optstring is a string of recognized option letters (see getopt(3)); if a letter is followed by a colon, the option is expected to have an argument which may or may not be separated from it by white space. The special option '--' is used to delimit the end of the options. The getopt utility will place '--' in the arguments at the end of the options, or recognize it if used explicitly. The shell arguments ($1 $2 ...) are reset so that each option is preceded by a '-' and in its own shell argument; each option argument is also in its own shell argu- ment. EXIT STATUS
The getopt utility prints an error message on the standard error output and exits with status > 0 when it encounters an option letter not included in optstring. EXAMPLES
The following code fragment shows how one might process the arguments for a command that can take the options -a and -b, and the option -o, which requires an argument. args=`getopt abo: $*` # you should not use `getopt abo: "$@"` since that would parse # the arguments differently from what the set command below does. if [ $? -ne 0 ]; then echo 'Usage: ...' exit 2 fi set -- $args # You cannot use the set command with a backquoted getopt directly, # since the exit code from getopt would be shadowed by those of set, # which is zero by definition. while true; do case "$1" in -a|-b) echo "flag $1 set"; sflags="${1#-}$sflags" shift ;; -o) echo "oarg is '$2'"; oarg="$2" shift; shift ;; --) shift; break ;; esac done echo "single-char flags: '$sflags'" echo "oarg is '$oarg'" This code will accept any of the following as equivalent: cmd -aoarg file file cmd -a -o arg file file cmd -oarg -a file file cmd -a -oarg -- file file SEE ALSO
getopts(1), sh(1), getopt(3) HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Behavior believed identical to the Bell version. Example changed in FreeBSD version 3.2 and 4.0. BUGS
Whatever getopt(3) has. Arguments containing white space or embedded shell metacharacters generally will not survive intact; this looks easy to fix but is not. Peo- ple trying to fix getopt or the example in this manpage should check the history of this file in FreeBSD. The error message for an invalid option is identified as coming from getopt rather than from the shell procedure containing the invocation of getopt; this again is hard to fix. The precise best way to use the set command to set the arguments without disrupting the value(s) of shell options varies from one shell ver- sion to another. Each shellscript has to carry complex code to parse arguments halfway correctly (like the example presented here). A better getopt-like tool would move much of the complexity into the tool and keep the client shell scripts simpler. BSD
January 26, 2011 BSD
All times are GMT -4. The time now is 10:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy