getopts :- expecting required input format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts :- expecting required input format
# 1  
Old 04-06-2011
getopts :- expecting required input format

The requirement is, i'm using getopts for calling two different function depending upon the input the user gives. Two functions are
1. help
2. processing function

these two functions are written inside the script and if the user gives only the below format the processing function should be called else only help function need to be called.

syntax format : sh script <YYYY-MM-DD-HH> <number>
format : sh scriptname.sh 2011-03-04-00<space-separated>6

please help me out with getopts code how it should be.
# 2  
Old 04-06-2011
# 3  
Old 04-06-2011
MySQL

Hi
maybe try this Smilie

# help function
Code:
# sh script -h
calling help function.....

# if false usage
Code:
# sh script -f "2011-03-04 1"
Usage: 'sh script -f "<YYYY-MM-DD-HH> <number>"' [for function use]
       'sh script -h'                            [for help]

# correct usage
Code:
# sh script -f "2011-03-04-00 6"
processing function with 2011-03-04-00 6........

# script
Code:
# cat script
## justdoit ##
 
#!/bin/bash
 
goto_fon () {
if [[ $(echo "$1" | grep -w '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\} [0-9]') ]] ; then
 echo "processing function with $1..."
 echo ".............................."
else
 goto_help ;
fi
}
 
goto_help () {
case "$1" in
   help) echo "viewing help menu....." ;;
   *) echo "Usage: 'sh $0 -f \"<YYYY-MM-DD-HH> <number>\"' [for function use]
       'sh $0 -h'                            [for help] " ;;
   esac
}
 
while getopts  "f:h" opt
do
  case "$opt" in
  f) goto_fon "$OPTARG" ;;
  h) goto_help "help" ;;
  *) goto_help "" ;;
  esac
done

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required in printing in specific format

Hi All, I 'm matching two files based on the first 2 columns and then populate other fields along with subtraction of few fields. I have managed to get the output. However, is there a easier way in formatting the output as shown below instead of using additional printf for getting fixed width... (4 Replies)
Discussion started by: shash
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Moderator input required please

We have been told to use this method for contacting moderators (rather than PM). Can any moderator please checkout what member 'buyvpn' is up to. Thanks. ---------- Post updated at 11:19 AM ---------- Previous update was at 11:15 AM ---------- Advertising services on at least one post. (1 Reply)
Discussion started by: hicksd8
1 Replies

3. UNIX for Dummies Questions & Answers

help required in converting a file format

My file format: -------------------------------------------------- Complete Consistency Check Valid Area : VALID:VALID Started by : esanwad Started at : Thu Dec 11 16:04:46 2014 CNA version : R21H04_EC08 Check range : AREA VALID/VALID ... (4 Replies)
Discussion started by: Gautam Banerjee
4 Replies

4. Shell Programming and Scripting

Need help to format one txt file to required format

Hello Everyone, I have one source file which is genarated by SAP in different format(Which I've never seen). I need to convert that file to required format and I need to read this target file from Datastage to use this in my Jobs. So I do not have any other options except to use Unix script to... (4 Replies)
Discussion started by: Prathyu
4 Replies

5. Shell Programming and Scripting

Concatenating two files in required format

Firstly one of my mysql queries will yeild following output +-------+---------------------+-------------------+----------------------------------------------------------------------------+ | ID | PLATFORM | SORT_NAME | DESCRIPTION ... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Not able to convert the second column to required format

I have this file 103,7243534512111,NiaC1-02 105,720412845543550,NiaC2-00 105,720439254543351,NiaC200 105,720445724354315,Nia100 105,72044770454398,Nia100 105,720484154334546,Nia616 i want in this format insert into aildump values(103,'7243534512111','NiaC1-02'); I'm able to... (3 Replies)
Discussion started by: nikhil jain
3 Replies

7. Shell Programming and Scripting

Input required for telnet in script

Guru, I am trying to use telnet in unix script. And it's asking password after executing telnet command in script. But I don't want to have manual intervention over there. So is it possible to pass password for telnet from script itself. What I am doing right now is something like this. ... (4 Replies)
Discussion started by: gander_ss
4 Replies

8. Shell Programming and Scripting

can getopts be used to override user input in a script

I have a script which takes user input as options provided PS3="Select option to do deploy : " select OPTION in "eardeploy" "hotdeploy" do case $OPTION in "eardeploy" ) eardeploy.sh break;; "hotdeploy" ) ... (1 Reply)
Discussion started by: codeman007
1 Replies

9. UNIX for Advanced & Expert Users

password required when using input redirection

Hello, I need to change user and run some commands by using a script. lets say, I'm using su - someuser << start password required -----> how can I enter the password here command 1 command 2 command 3 command 4 start While trying to run this I got the following message: "standard... (2 Replies)
Discussion started by: Alalush
2 Replies

10. Shell Programming and Scripting

restricting user input as required

Hi, I want the user to enter only numeric values and also he should only enter 2 digits only ( eg 23 or 23 or 03 any 2 digits) For the above purpose how should i declare my variable ? integer value if I read 03 in variable value then it gives me error ...also user can enter n number... (4 Replies)
Discussion started by: dhananjayk
4 Replies
Login or Register to Ask a Question