getopts script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts script
# 1  
Old 03-02-2011
getopts script

Hi Guys.

i need some help.

I need to create a script using getopts as i need the information to build my current knowledge.

getopts works in either ksh or bash

im trying to build a script which will need to look like the below :

Code:
Zok (random script name)  [-n infactions (how many times script must loop)] [ -w word (word to echo according to -n) ] [-u for uppercase]

the default would be for "zok" to print "zobbo" three times in lower case.

can anyone help me please?
# 2  
Old 03-02-2011
What have you tried and where did you get stuck? There should be plenty of posts here in the forum and documentation on the web.

Check this:
https://www.unix.com/shell-programmin...parameter.html
# 3  
Old 03-02-2011
this is what i currently have... where im getting stuck is keeping the default. im ratehr new toscripting so gentle please Smilie.

this is just the first bit of the script, i still need to add how many times it loops and the arg to make uppercase. i hope im making sense.

Code:
 
#echo $@
word='zobo'
while getopts w name
do
 case $name in
   w)   word=$name;;
  esac
done
shift $(( $OPTIND -1))
#echo $@
echo "arg1=$1 arg2=$2"
word=$name
echo $word

g=1
while [ $g -lt 4 ]
do
echo "$word" $g
g=$(($g + 1))
done
echo "end of script"

# 4  
Old 03-02-2011
Try this :
Code:
#!/bin/ksh
word='zobo'
num=3
upper=0
while getopts n:w:u option $*
do
  case $option in
    w) word=$OPTARG ;;
    n) num=$OPTARG ;;
    u) upper=1 ;;
  esac
done
echo $word $num $upper

The trick is to initialize the variables and modifiy them if the option is here

Last edited by Dahu; 03-02-2011 at 06:23 AM.. Reason: add comment
This User Gave Thanks to Dahu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

2. Shell Programming and Scripting

[KornShell]: Why getopts doesn't detect disabled switches in this script?

Hi, My environement OS: Linux Fedora Core 17 X86_64 KSH version: sh (AT&T Research) 93u+ 2012-08-01 As I understand inside a Kornshell script, the getopts statement allows to collect information about the switches provided for the script (if any). Besides, it is possible to... (3 Replies)
Discussion started by: dariyoosh
3 Replies

3. Shell Programming and Scripting

Using getopts. Need help

Hi all... I have been looking on here for the past few days for an answer and Im gonna have to break down and ask. I just learned about the getopts command last week so have been trying to utilize it in my scripts. Below, I am trying to set up a case structure for options using getopts.... (1 Reply)
Discussion started by: losingit
1 Replies

4. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

5. UNIX for Dummies Questions & Answers

Getopts

Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

6. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

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

8. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

9. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies

10. Shell Programming and Scripting

getopts

I have a script that facillitates NDM (Connect::\Direct) transfer to remote hosts. This script uses getopts to parse through the parameters passed to it and to set appropriate variables based upon what was passed in. Kickoff="mv $PATH/$FILE1 $PATH/$FILE2" ndm_shell.ksh -p $Node -s $Source -d... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question