variable option not properly recognized


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable option not properly recognized
# 1  
Old 12-15-2011
Data variable option not properly recognized

I'm creating a junk script which utilizes either -l or -p to list files or remove files, respectively, in the junk directory. When I run this code, inputting '-p' is unrecognized through the whole if/else block and falls to the last else (echo do nothing). In addition, I switched tests and tested for '-p' first, seeing if I could uncover hints, and inputting '-l' still made it catch on that first test even though it was testing for '-p'! (with input of -p still falling through.)
So confused. I've tried different quoting schemes, but maybe not the right one.


Code:
#! /usr/bin/sh

if [ ! -n "$1" ]
then
  echo "Usage: junk -l | -p | {filename}*"
else
  if [ "$1" -eq "-l" ]  # Test for '-l' input. Will always stop here, even if testing for '-p'
  then
    echo "The -l option was given"
    echo "The contents of the directory will be listed"
  else
    if [ "$1" -eq "-p" ]  # Test for '-p' input
    then
      echo "The -p option was given"
      echo "Directory and all files will be removed"
    else
      echo "do nothing"  # '-p' always fall through to here.
    fi
  fi
fi
exit 0

# 2  
Old 12-15-2011
-eq test option is for testing numeric values for strings use =

Code:
if [ "$1" = "-l" ]

You should consider using getopts:

Code:
while getopts lp opt
do
    case $opt in
        l) LIST_MODE=1 ;;
        p) DEL_MODE=1 ;;
        *) echo "Illegal option" ; exit 2 ;;
    esac
done
 
if [ -z "$LIST_MODE$DEL_MODE" ]
then
    echo "do nothing"
    exit 3
fi
if [ "$LIST_MODE" = "1" ]
then
    echo "List mode"
else
    echo "Del mode"
fi

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 12-15-2011
Thanks! That fixed it. getopts seems very useful. When I am not working on class assignments I think that would be more straightforward to use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing a variable value to an option

Hello, I am new to shell (i.e. linux bash) programming and have the following question: When using this wget command I can download a certain website that needs login information by passing a previously acquired cookie: wget --header='Cookie: SID=ac658ef0876b24ff456' somewebsite.comAs... (5 Replies)
Discussion started by: iggy98
5 Replies

2. Shell Programming and Scripting

Variable inside -name option for find command

Hi, I am not able to get output for find command if there are variables defined inside -name option . Please check below example 1) ###VARIABLES DEFINED process="fast" temperature="125c" voltage="0p935v" 2) I don't get output for below find command find -L <PATH> -type f \( -name... (2 Replies)
Discussion started by: gujrathinr
2 Replies

3. UNIX for Dummies Questions & Answers

~c is not working properly with -r option

Hi There, --------- file1 ------- ~c asd@ac.com -------------- Now i am using below command cat file1|mailx -s " testing" -r " My Name" abc@tech.com (3 Replies)
Discussion started by: Tapan Sharma
3 Replies

4. Shell Programming and Scripting

How to use a variable as a command option?

I am just learning shell scripting and already I found out I have the bad habit of thinking that it is similar to php or c. I learned some basics and now encountered this problem: On shell it is possible to type: $ date --date="2009-10-10 09:08:34" Sat Oct 10 09:08:34 CEST 2009 ... (2 Replies)
Discussion started by: quinestor
2 Replies

5. Shell Programming and Scripting

cp not dealing with variable properly? please help

I am having trouble with a script that is supposed to : a)take all the jpg pictures in a given directory/parameter and create thumbnails of it in a directory on the desktop. e.g from /here/are/the/files.jpg to ~/Desktop/parser-the/files.png this is my script: all the individual parts... (2 Replies)
Discussion started by: orochinagi
2 Replies

6. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

7. Shell Programming and Scripting

AWK: pattern not properly stored in variable?

Hey there, I have a table of contents file of the form 1 Title1 1.1 Subtitle1 1.1.1 Subsubtitle1 1.1.2 Subsubtitle2 ... and want to count the number of dots in the first field to find out the level of the section. I use the gsub function for the job, which works if I pass the pattern... (2 Replies)
Discussion started by: herrsimon
2 Replies

8. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

9. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Shell Programming and Scripting

Env Variable substituion in Sed (-s option)

Folks, I've been trying to use the ENV variable with slashes(/) in its value inside the sed substitution.. Sed 's/myval/'$MYVAL'/' file1 >> file.tmp If MYVAL=<sometext>, it works. if MYVAL=/home/venkat, it doesnt. *************************** bash-2.05$ export VAL=/home/venkat... (5 Replies)
Discussion started by: gvsreddy_539
5 Replies
Login or Register to Ask a Question