Beginner here, how to call a bash-script from python properly?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginner here, how to call a bash-script from python properly?
# 1  
Old 03-15-2016
Beginner here, how to call a bash-script from python properly?

Hi everyone,
i have the following script.sh:
Code:
foo='lsusb | grep Webcam | cut -c16-18'
sudo /home/user/public/usbreset /dev/bus/usb/001/$foo

when i try to call this script from python using
Code:
subprocess.call("script.sh", shell=True)

it seems that only 'sudo /home/user/public/usbreset' is being executed. how can i fix this properly?
Thanks in advance!
# 2  
Old 03-15-2016
How do you tell that only the quoted line is executed?
# 3  
Old 03-15-2016
uhm sorry, i meant to say that usbreset without the parameter
/dev/bus/usb/001/$foo
is being executed.
# 4  
Old 03-15-2016
Is the entire parameter missing or is just the $foo empty?
# 5  
Old 03-15-2016
well, your question has given me the answer as i used set -x to enable echo now, and the command is being executed as
Code:
 
sudo /home/user/public/usbreset /dev/bus/usb/001/lsusb | grep Webcam | cut -c16-18

So maybe i can use | to combine both commands into one line?
# 6  
Old 03-15-2016
You used apostrophes ' in lieu of backticks ` for the command substitution, so it failed and you had the literal string for the parameter.

---------- Post updated at 12:42 ---------- Previous update was at 12:40 ----------

And yes, you could use
Code:
sudo /home/user/public/usbreset /dev/bus/usb/001/$(lsusb | grep Webcam | cut -c16-18)

Backticks are deprecated and should be replaced by $( ... ) for command substitution.
# 7  
Old 03-15-2016
thanks a lot!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use scl enable python command call with in bash shell script?

I have a bash shell script, within it i am using the below two commands . its failing right on scl enable command itself. if i run it by itself without a shell, it works fine. by default it is using pythin version 2.6 something. i want to use 3.4 version for that reason with in the shell... (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

3. UNIX for Advanced & Expert Users

Calling expect from shell script which inturn call python

Hi Team, I have to execute a task from my local machine, where i keep my .expect,.sh, .bash and .python scripts .Task are coded in the script and has to be executed at remote machine. for that i used following task ..... SCRIPT 1: cat shell_check.sh read value if then expect... (3 Replies)
Discussion started by: Sivarajan N
3 Replies

4. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

5. Shell Programming and Scripting

[Solved] Unable to call a python script from bash

Hi, I am trying to run a python script embedded in bash script. But is throwing me an error. Please help. Script: #!/bin/bash nohup /usr/bin/python /opt/web/http.py & Error: /usr/bin/python: can't open file '/opt/web/http.py': No such file or directory Please help me on this. (6 Replies)
Discussion started by: maddy26615
6 Replies

6. Shell Programming and Scripting

Bash Script to Ash (busybox) - Beginner

Hi All, I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder. But... (5 Replies)
Discussion started by: sgtbobie
5 Replies

7. Shell Programming and Scripting

Python call to bash script returns empty string

Hi all, I need help figuring out why my script won't work when envoked from web interface. First off, I'm fairly new to Linux and Shell scripting so, my apologies for being ignorant! So I have a python script that I envoke through a web interface. This script envokes my shell script and... (8 Replies)
Discussion started by: arod291
8 Replies

8. Shell Programming and Scripting

Moving old files bash script - not working properly

I'm trying to write a script that moves data that's older than 2 weeks to a different place. It works well, EXCEPT, that when the script hits a file within a directory inside the working directory, it will move it to the root of the destination directory instead of putting it in the correct... (1 Reply)
Discussion started by: ugolee
1 Replies

9. Shell Programming and Scripting

how to call a bash script using perl

Hi I m new to perl. I m trying to write a perl script that calls a bash script; does anyone have a script already that they can provide or help me out? Thanks a lot. (2 Replies)
Discussion started by: adnan786
2 Replies

10. HP-UX

pstat_getdisk() call doesn’t work properly in HPUX 11.31 (11i V3)

As per the man page, pstat_getdisk() call returns the number of instances, which could be 0 upon successful completion, otherwise a value of -1 is returned. Please have a look at this sample program -> #include <stdio.h> #include <sys/pstat.h> int main() { int j = 0, ret; struct... (2 Replies)
Discussion started by: sandiworld
2 Replies
Login or Register to Ask a Question