ksh script that will accept arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script that will accept arguments
# 1  
Old 09-30-2011
Question ksh script that will accept arguments

Hi,

I am not very skilled using ksh scripts.

How do I create a ksh script that will accept arguments and use them in the script ?

I need to make this:

Run this command with this argument:
Code:
./mykshprogram.ksh madsen

and sometimes I need to do this:
Run the ksh again with 2 arguments:
Code:
./mykshprogram.ksh madsen jensen


So I need something like this:

Code:
if argument 2 is empty
do
 /usr/tools/run.exe -i madsen
else
 /usr/tools/run2.exe -i madsen -y jensen


Thanks in advance

Last edited by radoulov; 09-30-2011 at 11:17 AM.. Reason: Code tags.
# 2  
Old 09-30-2011
Maybe:
Code:
if [ $# -lt 2 ]
then
   /usr/tools/run.exe -i $1
else
   /usr/tools/run2.exe -i $1 -y $2
fi

# 3  
Old 09-30-2011
Code:
if [ -z $* ];then
/usr/tools/run.exe -i madsen
elif [[ -n $1 && -z $2 ]];then
 /usr/tools/run2.exe -i $1
elif [[ -n $1 && -n $2 ]];then
 /usr/tools/run2.exe -i $1 -y $2
fi

edit: i thought you said if all args were empty , thats why i added 1st if.
# 4  
Old 09-30-2011
For more robust command line argument processing, read up on the "getopts" program for parsing command line options. Here is one example I found that addresses this:
KSH - Sample program - Scroll down a few examples to see the example that uses getopts.

Gary

Last edited by gary_w; 09-30-2011 at 11:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh parsing arguments in a string rather than from the cmdln

Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks. My code is as follows: typeset... (6 Replies)
Discussion started by: user052009
6 Replies

2. Shell Programming and Scripting

Oracle/SQLPlus help - ksh Script calling .sql file not 'pausing' at ACCEPT, can't figure out why

Hi, I am trying to write a script that calls an Oracle SQL file who in turns call another SQL file. This same SQL file has to be run against the same database but using different username and password at each loop. The first SQL file is basically a connection test and it is supposed to sort... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. UNIX for Dummies Questions & Answers

UNIX script can accept 1 to n parameters

Just for my leaning purpose, I appreciate if someone answer my question: A UNIX script can accept 1 to n parameters. For each of these parameters, write out the parameter id number and its value. (1 Reply)
Discussion started by: shumail
1 Replies

4. Homework & Coursework Questions

Make a file accept only two arguments from the command line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) The script is executed in the Korn shell. 2) Name the shell script file is asg6s. 3) The asg6s file is... (7 Replies)
Discussion started by: ProgMan2015
7 Replies

5. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

6. Shell Programming and Scripting

Running local script remotely with arguments in ksh

When i use ssh command to execute local script on remote server , I am unable to do it. Please let me know how this can be done in ksh req=abc dte=ghd ssh username@hostname "$req $dte" < run_script.sh (2 Replies)
Discussion started by: lalitpct
2 Replies

7. Shell Programming and Scripting

How to accept arguments in shell script when calling in perl

I have a shell script like this: #!/bin/sh $PYTHON MetarDecoder.py < ../data/mtrs/arg1/arg2 And I'm calling it with this in perl: my $output = `./metar_parse.sh --options`; It's successful when I put in actual values for arg1 and arg2 in the shell script, but I'd like to pass arguments... (1 Reply)
Discussion started by: civilsurfer
1 Replies

8. UNIX for Dummies Questions & Answers

script unix which accept two arguments

does anyone can help me with this homework, please..I am beginner in linux and I don't how to do it :( Create a script scanner.sh which will accept two arguments: the first argument is the DNS name or the IP address of a system, or a network address or an IP range, the second argument is... (1 Reply)
Discussion started by: gennyy
1 Replies

9. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies

10. Shell Programming and Scripting

Writing Unix script to accept arguments

Hi, This may be answered elsewhere but I wasn't entirely sure of the wording I should use to search so here we go with an attempt: I wish to make a script that will allow commands to be passed to it such as: <command> -oOPTIONS -aANOTHER -pRINT etc However I don't really know the... (3 Replies)
Discussion started by: stuaz
3 Replies
Login or Register to Ask a Question