Sponsored Content
Top Forums Shell Programming and Scripting Getopts inside a function is not working Post 302836973 by prasperl on Thursday 25th of July 2013 01:48:17 AM
Old 07-25-2013
Getopts inside a function is not working

Hi All,

I am using geopts inside a function in shell script.

But it is doesnt seem to read the input args and I always gt empty value in o/p.

my code is
Code:
Image 


This message has not been sent.
#!/bin/ksh IFS=' ' readargs(){ OPTION=$1 OPTIND=1 while getopts "i:n:o" OPTION do case $OPTION in i) input="$OPTARG" ;; n) nput="$OPTARG" ;; o) output="$OPTARG" ;; esac done echo "i is $input o is $output n is $nput\n" } for i in `cat $1` do scriptname=`echo

Draft




This message has not been sent.

ActionsImage
Click here to continue working on this message.


 


Thursday, July 25, 2013 11:08 AM


ImageImageImage




#!/bin/ksh
IFS='
'
readargs(){
OPTION=$1
OPTIND=1
while getopts "i:n:o" OPTION
do
        case $OPTION in
                    i)
                       input="$OPTARG"
                       ;;
                    n)
                       nput="$OPTARG"
                       ;;
                    o)
                       output="$OPTARG"
                      ;;

        esac
done
echo "i is $input o is $output n is $nput\n"
}
for i in `cat $1`
do
    scriptname=`echo $i|awk '{print $1}'`
    stringopts=`echo $i|awk '{$1="";print}'`
    echo $scriptname
    readargs $stringopts
done


Data looks like this
Code:
>cat file.txt
a.ksh -o hi -i hello -n how
c.ksh -i sam -n mi -o ki

Anything wrong with my code?

Last edited by jim mcnamara; 07-25-2013 at 08:09 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

2. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies

3. Shell Programming and Scripting

value inside a function

i have a script like this #!/bin/ksh initialize() { x=20 } ... ... ... x=10 initialize; echo $x (2 Replies)
Discussion started by: trichyselva
2 Replies

4. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

5. Shell Programming and Scripting

getopts function in Perl

Hi All I have searches getopts function in Perl a lot, but yet i didn't cleared with it. First I want to know what is the meaning of getopts('t:c:', \%options); and please explain getopts function in an easy way.. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

6. Programming

Changing value inside function

I have the following code and want to update shiftDesc inside the function. Is it correct to declare the argument as: int shiftDesc void prValue_vd( FILE* stream, // name of output stream int shift, // amount of shift to the right const char* value,... (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

8. Shell Programming and Scripting

If loop inside function not working.

check_deplver () { dir=/abc/def/ghi if ssh -o StrictHostKeychecking=no $1 "" 2> /dev/null then echo " output is " ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc" 2> /dev/null else echo " directory not presnt" fi } This is not working. But... (7 Replies)
Discussion started by: NarayanaPrakash
7 Replies

9. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

10. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
GIT-SH-SETUP(1) 						    Git Manual							   GIT-SH-SETUP(1)

NAME
       git-sh-setup - Common Git shell script setup code

SYNOPSIS
       . "$(git --exec-path)/git-sh-setup"

DESCRIPTION
       This is not a command the end user would want to run. Ever. This documentation is meant for people who are studying the Porcelain-ish
       scripts and/or are writing new ones.

       The git sh-setup scriptlet is designed to be sourced (using .) by other shell scripts to set up some variables pointing at the normal Git
       directories and a few helper shell functions.

       Before sourcing it, your script should set up a few variables; USAGE (and LONG_USAGE, if any) is used to define message given by usage()
       shell function. SUBDIRECTORY_OK can be set if the script can run from a subdirectory of the working tree (some commands do not).

       The scriptlet sets GIT_DIR and GIT_OBJECT_DIRECTORY shell variables, but does not export them to the environment.

FUNCTIONS
       die
	   exit after emitting the supplied error message to the standard error stream.

       usage
	   die with the usage message.

       set_reflog_action
	   Set GIT_REFLOG_ACTION environment to a given string (typically the name of the program) unless it is already set. Whenever the script
	   runs a git command that updates refs, a reflog entry is created using the value of this string to leave the record of what command
	   updated the ref.

       git_editor
	   runs an editor of user's choice (GIT_EDITOR, core.editor, VISUAL or EDITOR) on a given file, but error out if no editor is specified
	   and the terminal is dumb.

       is_bare_repository
	   outputs true or false to the standard output stream to indicate if the repository is a bare repository (i.e. without an associated
	   working tree).

       cd_to_toplevel
	   runs chdir to the toplevel of the working tree.

       require_work_tree
	   checks if the current directory is within the working tree of the repository, and otherwise dies.

       require_work_tree_exists
	   checks if the working tree associated with the repository exists, and otherwise dies. Often done before calling cd_to_toplevel, which
	   is impossible to do if there is no working tree.

       require_clean_work_tree <action> [<hint>]
	   checks that the working tree and index associated with the repository have no uncommitted changes to tracked files. Otherwise it emits
	   an error message of the form Cannot <action>: <reason>. <hint>, and dies. Example:

	       require_clean_work_tree rebase "Please commit or stash them."

       get_author_ident_from_commit
	   outputs code for use with eval to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL and GIT_AUTHOR_DATE variables for a given commit.

       create_virtual_base
	   modifies the first file so only lines in common with the second file remain. If there is insufficient common material, then the first
	   file is left empty. The result is suitable as a virtual base input for a 3-way merge.

GIT
       Part of the git(1) suite

Git 2.17.1							    10/05/2018							   GIT-SH-SETUP(1)
All times are GMT -4. The time now is 03:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy