1

Hi I am trying to make a shell script to accept arguments at startup. The goal I want to achieve is to call a python script which by itself takes arguments. If the arguments are missing, default statement to make them predefined default values. This is the script I am trying to make work:

#!/bin/bash
#Script for calling th e python script sniff.py

while getopts d:D o
do case "$o" in
d)  usb_dev="@OPTARG";;
D)  console_outp="@OPTARG";;
*)  usb_dev=ttyUSB1;
    console_outp=INFO;      
esac
done

sniff=/home/user/Documents/Wireshark/Sensniff/sniff.py

sudo python $sniff -d "$usb_dev" -D "$console_outp"
sleep 5d

This script of course isn't working. Can you please help me to make this script working?

  • You're absolutely right, my answer didn't relate at all; I somehow missed the fact that you were using getopts – kos Sep 14 '15 at 9:09
  • Can you point out what's not working exactly? On a side note, I'm sure you can parse the argument within the Python script using some Python function / built-in (if this works for your case). – kos Sep 14 '15 at 9:11
1

try

#!/bin/bash
#Script for calling th e python script sniff.py

usb_dev=ttyUSB1
console_outp=INFO

while getopts d:D: o
do case "$o" in
d)  usb_dev="$OPTARG";;
D)  console_outp="$OPTARG";;
esac
done

(same as your script)
  • default value should be initialized before.
  • use $OPTARG instead of @OPTARG
  • option are defined as :D:d:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.