2

I have two python lines in my bash script, both should run with -c flag, but once I run the Bash script it tells me that it could not find -c flag that is needed for python to run the command in Bash. It does work from command like if I copy the python command to command line it runs the command, but not from inside the script.

Error output:

mount.sh: 40: mount.sh: -c: not found

My script:

## define a function that launched the zenity username dialog
get_username(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Username:"
}
# define a function that launched the zenity password dialog
get_password(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Password:" --hide-text
}

# attempt to get the username and exit if cancel was pressed.
wUsername=$(get_username) || exit

# if the username is empty or matches only whitespace.
while [ "$(expr match "$wUsername" '.')" -lt "1" ]; do
    zenity --error --title="Error in username!" --text="Please check your username! Username field can not be empty!"  || exit
    wUsername=$(get_username) || exit
done

wPassword=$(get_password) || exit

while [ "$(expr match "$wPassword" '.')" -lt "1" ]; do
    zenity --error --title="Error in password!" --text="Please check your password! Password field can not be empty!" || exit
    wPassword=$(get_password) || exit
done

python -c 'import keyring; keyring.set_password("WinMount", wUsername, wPassword)'

Get_wPassword=python -c 'import keyring; keyring.get_password("WinMount", wUsername)'

# mount windows share to mountpoint
sudo mount -t cifs //$SERVER/$SHARE ${HOME}/${DIRNAME} -o username=${wUsername},password=$Get_wPassword,domain=${DOMAIN}
2
  • Whats your shebang line in the script? #!/bin/bash?
    – chaos
    May 21, 2014 at 13:47
  • Yes its #!/bin/bash.
    – cr0c
    May 21, 2014 at 13:51

3 Answers 3

4
Get_wPassword=python -c 'import keyring; keyring.get_password("WinMount", wUsername)'

should probably be

Get_wPassword=$(python -c 'import keyring; keyring.get_password("WinMount", wUsername)')

and if you want wUsername to be given by the shell variable with the same name

Get_wPassword=$(python -c "import keyring; keyring.get_password('WinMount', '$wUsername')")

(note how the double quotes became single and vice versa)

5
  • This works thanks :) But how can I make python to take the wUsername from the bash, because it says its undefined at the moment, but I do write it in the zenity and I want it to take it from the zenity line after its inserted.
    – cr0c
    May 21, 2014 at 14:00
  • See my edit....
    – fkraiem
    May 21, 2014 at 14:23
  • I have tried the way you edited it. It did not work. It says its undefined. See chaos post to right answer.
    – cr0c
    May 21, 2014 at 14:24
  • Ah sorry, you need to put quotes around $wUsername too, of course. Using os.getenv for that is way overkill, but whatever...
    – fkraiem
    May 21, 2014 at 14:28
  • Your solution works as well. Because you have less rep I made your's the answer
    – cr0c
    May 21, 2014 at 14:30
4

It's because of that line:

Get_wPassword=python -c 'import keyring; keyring.get_password("WinMount", wUsername)'

-c is interpreted as a command. Write it as follows:

Get_wPassword=$(python -c 'import keyring; keyring.get_password("WinMount", wUsername)')

EDIT: For the problem with the bash variable. You can use os.getenv to access environment variables from Python:

export wUsername
export wPassword
Get_wPassword=$(python -c "import keyring; import os; \
keyring.get_password("WinMount", os.getenv('wUsername'))")
unset wPassword
1
  • +1 for os.getenv, this will work even if 'wUsername' has quotes in it.
    – daboross
    May 21, 2014 at 20:52
2

The line

Get_wPassword=python -c 'import keyring; keyring.get_password("WinMount", wUsername)'

means "Run -c with $Get_wPassword set to python"

To just set $Get_wPassword to the the whole command use

Get_wPassword='python -c \'import keyring; keyring.get_password("WinMount", wUsername)\''

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .