Passing arguments to python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to python
# 1  
Old 10-13-2010
Passing arguments to python

How can I pass arguments to a python script???
# 2  
Old 10-13-2010
Hi,

I think you execute:
Code:
$ python myscript.py <arg1> <arg2> ... <arg-n>

and use 'sys.argv' to read them.

I hope this helps.

Regards,
# 3  
Old 10-13-2010
Yes but having a problem on how to use sys.argv to check every argument against a set of options. I'm just starting playing in python as I would need to use arithmetic computations which are messy to do using csh.

The arguments passed will be something like this:

Code:
python myscript.py -FDRW1=file1 -FDRW2=file2- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 	-->

I get each arguments e.g. -FDRW1=file1 and seperate the two things on either side of '='.

Have got something like this coded in a csh script. Need to do something similar.

Code:
  set narg = $#argv
  while ($iarg < $narg)

    MATH iarg = $iarg + 1
    set arg = $argv[$iarg]
    set opt = ` echo $arg | awk 'BEGIN { FS="=" } { print $1 }' `
    set par = ` echo $arg | awk 'BEGIN { FS="=" } { print $2 }' `

# Change options to upper case.
# Use tr "[A-Z]" "[a-z]" to change from upper to lower case)
    set opt = ` echo $opt | tr "[a-z]" "[A-Z]" `
    echo "opt = $opt"

    switch ($opt)
    case "-FDRW1":
        set Afdrw1 = $par
        set fdrw1 = `echo $Afdrw1 | awk 'BEGIN {FS=".log"} {print $1}'`
        set nxz = `echo $fdrw1 | awk 'BEGIN{FS="-"} {print $NF}'`
        set optfdrw1 = 1
        set optfdrw = 1
        breaksw
    case "-FDRW2":
        set Afdrw2 = $par
        set fdrw2 = `echo $Afdrw2 | awk 'BEGIN {FS=".log"} {print $1}'`
        set optfdrw2 = 1
        set optfdrw = 1
        breaksw
   default:
      set Aerr = $arg
      set ierr = 1
      breaksw
    endsw

  end   # while

# 4  
Old 10-13-2010
Hi,

You should take a look to the 'getopt' or 'optparse' modules. I think it is what you need.

Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

2. Programming

Passing arguments to shellcode

Is there any way I could pass arguments to shellcode. My goal is to store a program in a image file, and have another program read and run the code with arguments in memory. Currently I can store a program in a image file, then read it back to the hard-drive run it normally then delete it when... (5 Replies)
Discussion started by: image28
5 Replies

3. Shell Programming and Scripting

Problem passing arguments to a Python script

I have part of the script below and I tried calling the script using ./tsimplex.py --fstmod=chris.cmod --nxz=8x6 --varp=0.25 but am getting the error option --fstmod must not have an argument Any idea on how to fix this would be highly appreciated #! /usr/bin/python import... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

Passing arguments to csh

I have noticed this thing using csh when passing arguments Suppose I call a csh script using ../Scripts/plot-model.csh -vmod="npt02-z30.vmod" -R="0/80/0/30" -c="0/4.5" -aspr="1:10" Somehow the " get removed when doing $argv ending up with -vmod=npt02-z30.vmod... (0 Replies)
Discussion started by: kristinu
0 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

6. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

7. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

8. UNIX for Dummies Questions & Answers

passing strings as arguments

Is it possible to pass a string as an argument from the command line? I know I can pass a word in but can I put a line of text in with spaces and fullstops or do I just put it in brackets or quotes so the compiler can differinate between the first argument and the second. (1 Reply)
Discussion started by: iago
1 Replies

9. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question