The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2009
naminator naminator is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
cleandisk and awk

Hi guys,

I have a script that i done but now i wanna do something like this .
The name of the script is cleandisk and it has some options.

cleandisk -I -a
cleandisk -I -v

When you type -I -a it should do something and its the same way to the other option, if you type only an argument like cleandisk -I , it should print a message , and when you type no argument it gives another message.

I tried with awk an if, but i think i´m not doing it right.

Thanks
  #2 (permalink)  
Old 03-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,372


Code:
ok=0
while getopts lucky opt
do
  case $opt in
    l|u|c|k|y) echo "You gave the -$opt option"; ok=1 ;;
  esac
done
shift $(( $OPTIND - 1 ))

if [ $ok -eq 0 ]
then
    echo "You didn't enter a valid option"
fi


Last edited by cfajohnson; 03-18-2009 at 12:25 PM.. Reason: Changed conflicting variable name: opt => ok
  #3 (permalink)  
Old 03-18-2009
naminator naminator is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 4
Thanks but

Thanks for the help but,

If you have to enter at least two arguments like cleandisk -I -V and if you type only one it gives some message.

Another thing if i type the dir i want after the arguments like:

cleandisk -I -V /home

how can i save the "/home" to a variable ?

Thanks,
  #4 (permalink)  
Old 03-18-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,372
Quote:
Originally Posted by naminator View Post
Thanks for the help but,

If you have to enter at least two arguments like cleandisk -I -V and if you type only one it gives some message.

Another thing if i type the dir i want after the arguments like:

cleandisk -I -V /home

how can i save the "/home" to a variable ?

It already is in a potitional parameter: $3

After processing the options, it will be in $1.


Code:
opts=IV ## Put the option letters you want to use in $opts
ok=0
while getopts "$opts" opt
do
  case $opt in
    I) echo option I; ok=$(( ok + 1 )) ;;
    V) echo option V; ok=$(( ok + 1 )) ;;
  esac
done
shift $(( $OPTIND - 1 )) ## remove options; /home will now be $1

if [ $ok -eq 0 ]
then
   echo You did not give any options >&2
   exit 1
else
   echo You entered $ok options
fi

if [ $# -gt 0 ]
then
   echo "The remaining arguments are:"
   printf "  %s\n" "$@"
else
   echo "There are no arguments"
fi

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0