Help with ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with ksh script
# 1  
Old 10-05-2017
Help with ksh script

Hello folks;
Thanks in advance for any help!

Please bear with me on this one. I need help writing a KSH script (must be in ksh) to do the following:

  • Return usage information if no arguments are given.
  • Always return 0, for success.
  • Take as arguments:
    • <filename:mandatory> A path to a file to be transferred.
    • <other: optional> Any number of var=val pairs supplied by the caller.
  • Make an HTTP POST to server A, to a specified URL.
    • The file specified will be sent as the POST data.
    • Any supplied optional var=val pairs will be sent as parameters in the URL.
  • The Content-Type header in the request should match the extension of the provided file. For an unknown type, the default should be “application/octet-stream”.

Last edited by rbatte1; 10-06-2017 at 07:46 AM.. Reason: Converted textual bullet list into formatted bullet list
# 2  
Old 10-05-2017
The standard questions/limitations apply here:
  • What is your OS?
  • Which version of ksh?
    Namely: do you use ksh88 or ksh93?
  • Is this homework/coursework?
    If yes, please re-post in the special forum for such questions, special rules apply there.

In general i see the following problematic provisions with your requirements:

Quote:
  • Take as arguments:
    • <filename:mandatory> A path to a file to be transferred.
    • <other: optional> Any number of var=val pairs supplied by the caller.
Note that there is a fine difference between "arguments", "options" and "arguments to options". If you want exactly one filename and any number of name=val pairs you might consider using an option for the filename (with the filename as argument to the option) and the name=val pairs as (open-ended list of) arguments to the script. A command line would look like this:

Code:
script -f /path/to/file var1=val1 var2=val2

This is easy to implement using the getopts-builtin of ksh. I suggest you read its man page. Provisions for automatically displaying some usage text if the parameters given are not valid is also easily done using getopts.

I hope this helps.

bakunin

Last edited by rbatte1; 10-06-2017 at 07:47 AM.. Reason: Retro fitted edits to quoted post
# 3  
Old 10-05-2017
Thanks Bakunin for getting back to me.
to answer your questions:
  1. The OS is RedHat
  2. I don't care which ksh we use but I think ksh93 is more advanced
  3. It's not homework or coursework.

I think passing the file name with one argument should work.

Last edited by rbatte1; 10-06-2017 at 07:44 AM.. Reason: Replaced textual numbered list with formatted numbered list, using LIST=1 tags
# 4  
Old 10-06-2017
Quote:
Originally Posted by Katkota
I think passing the file name with one argument should work.
Yes, until you have a filename of "var=val", which is possible. Of course you can first examine every argument passed and if a file with that name exists, take it as a filename and if not, then examine if it is a well-formed item of the form "var=val". This is possible but it is easier (read: saves programming effort) to distinguish (for the script) between filenames and arguments up front.

Quote:
Originally Posted by Katkota
I don't care which ksh we use but I think ksh93 is more advanced
Yes, but it won't matter that much here. It is quite possible to do it in both ksh-versions. Notice, though, that what some Linux-distributions (i have no experience with RedHat) have labeled as a Korn Shell is in fact not a real Korn Shell but something awful (mksh, pdksh, etc.).

I say that because one of my scripts just failed on a SLES 12 system, whereas it ran perfectly on SLES 11. Upon inspection we found that SLES 12 has only "pdksh" (which is a mixture of a bash-like shell and some ksh-isms but not even compatible with ksh88) but names it "ksh". In fact the original ksh is not even in the SLES-12-repository.

What have you tried so far and where were you stuck?

I hope this helps.

bakunin
# 5  
Old 10-08-2017
So far i have this. Any help making it better would be greatly appreciated (especially with the curl part..
Code:
#!/bin/ksh

# Check that we got args
if [ ${#} -eq 0 ]
then
   echo "Usage is ..."
   exit 255;
fi

# Get filename from first arg
PostFilename=$1
shift

# Handle Args
argNum=1
while [ $# -gt 0 ]
do
  echo "$1"
  argArray[argNum]=$1
  ((argNum=argNum+1))
  shift
done

# POST file
echo $PostFilename
echo ${argArray[@]}
# curl -X POST -d $PostFilename http://google.com ${argArray[@]}

# 6  
Old 10-09-2017
What's wrong with that? And, why don't you call it like
Code:
curl -X POST -d $PostFilename http://google.com $@

# 7  
Old 10-09-2017
Quote:
Originally Posted by Katkota
So far i have this. Any help making it better would be greatly appreciated (especially with the curl part..
I think what you have is a good start. Before i suggest code a few general remarks, though:

whatever a user supplies to a program (this is not restricted to scripts) should be tested for plausibility/formal correctness before the input is used. If you get a file name: test if the file is there and if it is readable. If you expect "arg=val" lines test if they are indeed of the form you suppose they are.

Another point is the handling of variables: long ago i started to declare every variable i use before i use it. I know, one doesn't need to do so in ksh, but i like the documentation which is automatically generated this way.

third, as i already mentioned, handling parameters/arguments is easier with getopts. This part:

Code:
#!/bin/ksh

# Check that we got args
if [ ${#} -eq 0 ]
then
   echo "Usage is ..."
   exit 255;
fi

# Get filename from first arg
PostFilename=$1
shift

# Handle Args
argNum=1
while [ $# -gt 0 ]
do
  echo "$1"
  argArray[argNum]=$1
  ((argNum=argNum+1))
  shift
done

done with getopts and a few plausibility checks might look like this:

Code:
#!/bin/ksh

f_usage ()
{
print -u2 - "Here goes the usage information...."

exit 1
}


f_CheckArg ()
{
# checks a single "arg=val" if it is correctly formed
# checks could be more elaborate than that:

if [ "${1}" == "${1#*=}" ] ; then          # something before the "="?
     return 1
fi
if [ "${1}" == "${1%=*}" ] ; then          # something after the "="?
     return 1
fi

return 0
}


# main ()

typeset chOpt=""                                 # option buffer
typeset chArgs="$*"                              # save cmd line
typeset fIn=""                                   # input file
# typeset achArgs[]                              # arg=val strings

while getopts ":h:f:v:" chOpt ; do               # commandline
     case $chOpt in
          "f")
               if [ -n "$fIn" ] ; then
                    print -u2 - "Do not specify more than one file."
                    exit 2
               fi
               if [ ! -r "$OPTARG" ] ; then
                    print -u2 - "File $OPTARG does not exist or is not readable."
                    exit 2
               fi
               fIn="$OPTARG"
               ;;

          "h")
               f_usage
               ;;

          "?")
               if [ "$chOpt" == "?" -a "$OPTARG" == "?" ] ; then
                    f_usage
               else
                    print -u2 - "unknown option -${OPTARG}"
               fi
               ;;

     esac
done
shift $(( OPTIND -1 ))

if [ -n "$fIn" ] ; then
     print -u2 - "No filename specified, aborting...."
     exit 2
fi

# Handle Args
while [ -n "$1" ] ; do
     if f_CheckArg "$1" ; then   
          typeset achArgs[((${#achArgs[@]}+1))]="$1"
     else
          print -u2 - "argument $1 is not well-formed"
          exit 3
     fi
     shift
done


Call the script like:

Code:
script -f /path/to/file arg1=val1 arg2=val2 ...

or
Code:
script -?

Code:
script -h

I hope this helps.

bakunin

Last edited by bakunin; 10-10-2017 at 05:22 AM.. Reason: typo corrected
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace lines in ksh Script

Hi All, I am novice to Unix and I need your expert advice for the below task. There is a KSH script file in which I need to replace few line as per the below expectations. So my file look like as # Host Setup Command: Line 1 Line 2 Line 3 Line 4 Line Any... (6 Replies)
Discussion started by: rupid0609
6 Replies

2. Shell Programming and Scripting

Deploy ksh script to file from other script

Hi all, I need to deploy two scripts on around ~100 machines and have only OPSware. Opsware have the option to execute a script, so I am trying to write a script which dose cat > script.ksh <<EOF script to be deployed EOF However the script between the two EOFs gets also executed which... (0 Replies)
Discussion started by: click
0 Replies

3. Shell Programming and Scripting

Help Create dynamic ksh script from a script

I am currently running 2 scripts to gather data for a 3rd script and would like to combine the 2 scripts into one. Having issues with the final output format. Note cannot post URL so replaced the http stuff with (name) in the examples All scripts contain #!/bin/ksh OS = Red Hat Enterprise... (0 Replies)
Discussion started by: pcpinkerton
0 Replies

4. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

5. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

6. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

7. Shell Programming and Scripting

tracing a ksh script within a ksh script

I normally trace a script with the ksh -x <script name> and redirect strderr to file. But if you have a script like the examble below...... vi hairy bear=`grep bear animals` if then ksh more_animals fi If I ksh -x hairy it won't trace "more_animals" unless I put a -x in it. Is... (1 Reply)
Discussion started by: shorty
1 Replies

8. Shell Programming and Scripting

how to convert unix .ksh script to windows .batch script

I am using awk in my .ksh script but when I am trying to run in windows its not recognising awk part of the ksh script , even when I changed it to gawk it does not work, this is how my .ksh and .bat files look like. thanx. #!/bin/ksh egrep -v "Rpt 038|PM$|Parameters:|Begin |Date: |End... (1 Reply)
Discussion started by: 2.5lt V8
1 Replies

9. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 Replies

10. UNIX for Dummies Questions & Answers

SQL Script run in KSH Script

I've got a SQL script that is executed through a UNIX ksh script. It is working fine, but I wanted to add a line to put a date/time stamp in the log file that it generates. This is more of a SQL question, but I'm hoping someone can help me get the date/time...I've changed the script with the... (2 Replies)
Discussion started by: dstinsman
2 Replies
Login or Register to Ask a Question