I have a Python program, it generates a string, say fileName. In fileName I hold something like "video1.avi", or "sound1.wav". Then I make an os call to start a program ! program arg1 arg2, where my fileName is arg2. How can I achieve that on the fly, without making the whole program return a single string (the fileName) and then pass it to the shell line? How can I make that during execution. The script executes in Jupyter.  P.S. I am looping and changing the file name and I have to run that script at every loop.
2579
How do you call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
4021
What is the best way to go about calling a function given a string with the function's name in a Python program.  For example, let's say that I have a module foo, and I have a string whose content is "bar". What is the best way to call foo.bar()?  I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.
80943
I want to pipe file names to other programs, but they all choke when the names contain spaces.  Let's say I have a file called.  foo bar   How can I get find to return the correct name?  Obviously I want:  foo\ bar   or:  "foo bar"   EDIT: I don't want to go through xargs, I want to get a correctly formatted string out of find so that I can pipe the string of file names directly to another program.
47297
I have a program that receives an argument and prints it every second:    ~# ./myapp 234 234 234 234 234 ...     I want to be able to run it in the background and right after a shell but it must be in one line (I don't want to explain why I need it because I don't want to make the discussion too complicated right now).    This is what I tried and the error I received:    ~# bash -c "./myapp 234 &amp;; sh"  bash: -c: line 0: syntax error near unexpected token `;' bash: -c: line 0: `./myapp 234 &amp;; sh'   I also tried with &amp;&amp; as separator:    ~# bash -c "./myapp 234 &amp; &amp;&amp; sh"  bash: -c: line 0: syntax error near unexpected token `&amp;&amp;' bash: -c: line 0: `./myapp 234 &amp; &amp;&amp; sh'   Any idea how I can solve it?    EDIT: Thanks to @roaima. I also found a link that talks about it:
30241
Similar problem as here  However, the solution given there is not universal.  My problem: I have a command that produces output to STDOUT. At the same time, I can specify via arguments to produce another output that goes straight to a file.  tcsh$ hmmscan --domtblout FILEOUTPUT Pfam-A.hmm query.fasta   Pfam-A.hmm is a database file (input); query.fasta is also an input file  How can I redirect FILEOUTPUT to STDOUT ?  Background: I plan to use the command on thousands of inputs and I need to run it for each of them separately. The command is called within a Python script, and I would like to read its FILEOUTPUT directly into the memory/a variable using the subprocess module. I know how to get the STDOUT with the subprocess module; so if I could redirect FILEOUTPUT to STDOUT...  I tried already stuff like this (ended up with errors or created files):  tcsh$ hmmscan --domtblout &gt;/dev/tty Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;(mypipe) Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;$ Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;($1) Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout &gt;cat Pfam-A.hmm query.fasta tcsh$ hmmscan --domtblout test.tab Pfam-A.hmm query.fasta &gt;/dev/tty
51024
When I am running two while loop and trying to concatenating two variable strings (from two loops), the final output (string) is weird. I have tried different way to concatenating but did not get the answer: Below is the code: [I need vfile="var1_Amon_My_model1*.nc"]  #!/bin/bash jcount=1 extn="*.nc" while read line do mname=$line echo " Working on model - " $mname echo " and model number = "$jcount while read line do vname=$line vfile="${vname}""_Anom_""${mname}""${extn}" echo $vfile done&lt;varlists.txt echo "******************************************" jcount=$((jcount+1)) done&lt;model_test1.txt   In varlists.txt  var1 var2   In model_test1.txt  My_model1 My_model2   After executing the script, it is showing   Working on model -  My_model1  and model number = 1 *.ncm_My_model1 *.ncm_My_model1 ******************************************
69962
I have an application which takes as an input attributes in double quotes embedded in single quotes. Take for example this right command:  command -p 'cluster="cl1"'   In order to automate it, I created a bash file using $CLUSTER as a variable. How should be my command? In other words, what should I put instead of cl1?  Please note that, If I modified the above command, it won't be accepted. For instance: command -p "cluster=cl1" is not accepted
41809
I have a string that I want to use as a filename, so I want to remove all characters that wouldn't be allowed in filenames, using Python.  I'd rather be strict than otherwise, so let's say I want to retain only letters, digits, and a small set of other characters like "_-.() ". What's the most elegant solution?  The filename needs to be valid on multiple operating systems (Windows, Linux and Mac OS) - it's an MP3 file in my library with the song title as the filename, and is shared and backed up between 3 machines.
121013
Exec allows us to either pass all arguments at once with {} + or to pass them one by one with {} \;  Now let's say I want to rename all jpeg, no problem doing this:  find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec mv '{}' '{}'.new \;   But if I need to redirect output, '{}' isn't accessible after redirection.  find . \( -name '*.jpg' -o -name '*.jpeg' \) -exec cjpeg -quality 80 '{}' &gt; optimized_'{}' \;   This wouldn't work. I'd have to use a for loop, storing find's output into a variable before using it. Let's admit it, it's cumbersome.  for f in `find . \( -name '*.jpg' -o -name '*.jpeg' \)`; do cjpeg -quality 80 $f &gt; optimized_$f; done;   So is there a better way?
5735
I must have a blind spot, but I cannot find what it is.   I made a small python script that removes VLC from the sound menu. It runs perfectly in any way I run it from the terminal or from a launcher or whatever you can think of.   What the script actually does is nothing more than get the get the current settings:  gsettings get com.canonical.indicator.sound interested-media-players   edit the list, and set the changed list by:  gsettings set com.canonical.indicator.sound interested-media-players "['newlist']"   These commands are executed by a python script,. However, when run from a cronjob (crontab -e) only the gsettings - get - part works, but not the gsettings - set - section. That the get section works fine with cron, I checked by making the script write data (the original as well as the edited) to an external file.  Not a python problem  To see if the problem is related to python code, I created a bash script that applies a changed sound menu items list. The story is the same: the bash script runs fine from the command line or a launcher, not from cron, while any other command in the same script would run fine. Also, if I add any command at the end of the script below, it works fine and it looks like the script is satisfied with its own work.  Why is the gsettings set command not working when initiated from cron?  This is the script:  #!/usr/bin/python3 import subprocess  def read_soundmenu():     # read the current launcher contents     get_menuitems = subprocess.Popen([         "gsettings", "get", "com.canonical.indicator.sound", "interested-media-players"         ], stdout=subprocess.PIPE)     return eval((get_menuitems.communicate()[0].decode("utf-8")))  def set_current_menu(current_list): # this takes no effect from cron     # preparing subprocess command string     current_list = str(current_list).replace(", ", ",")     subprocess.Popen([         "gsettings", "set", "com.canonical.indicator.sound", "interested-media-players",         current_list,         ])  current_list = read_soundmenu() for item in current_list:     if item == "vlc.desktop":         current_list.remove(item) set_current_menu(current_list)
37025
How do I open a file with the default associated program in Java? (for example a movie file)
121768
I'd like to do a one line bash command that automatically does this:  screen -S myserver  python myserver.py  # inside the new screen CTRL A, D (detach)   I think this won't work:  screen -S myserver &amp;&amp; python myserver.py   because python myserver.py won't be started inside the screen.
91176
The following works great on the command-line:  $ ffmpeg -i input.m4a -metadata 'title=Spaces and $pecial char'\''s' output.m4a   How do I parameterize this command and use it in a script/function? I would like to add multiple metadata tags like this:  $ set-tags.sh -metadata 'tag1=a b c' -metadata 'tag2=1 2 3'   update:  I simplified my question a little too much. I actually want to call a script that calls a script with with the parameterized command in it.  This is my exact use case:  This function converts files to audio-book format (defined in .profile):  # snippet of .profile convert_to_m4b () {     FILE="$1"     BASENAME=${FILE%.*}; shift      ffmpeg -i "$FILE" -vn -ac 1 -ar 22050 -b:a 32k "$@" tmp.m4a &amp;&amp;     mv tmp.m4a "$BASENAME.m4b" }; export -f convert_to_m4b   Function convert_to_m4b is called from download-and-convert.sh:  #/bin/sh  MP3_URL=$1; shift FILENAME=$1; shift  if [ ! -f "${FILENAME}.mp3" ]; then     curl --location --output "${FILENAME}.mp3" "$MP3_URL" fi  convert_to_m4b "${FILENAME}.mp3" "$@"   Download-and-convert.sh is called from process-all.sh:  #/bin/sh download-and-convert.sh http://1.mp3 'file 1' -metadata 'title=title 1' -metadata 'album=album 1' download-and-convert.sh http://2.mp3 'file 2' -metadata 'title=title 2' -metadata 'album=album 2' ... ... download-and-convert.sh http://3.mp3 'file N' -metadata 'title=title N' -metadata 'album=album N'   I get this error from ffmpeg:  [NULL @ 00000000028fafa0] Unable to find a suitable output format for ''@'' '@': Invalid argument   "$@" works if I inline convert_to_m4b in download-and-convert.sh instead of calling the function.    The following does not work because the quotes are lost, causing arguments with spaces to be incorrectly split up:  #/bin/sh ffmpeg -i input.m4a $@ output.m4a   I have tried various methods of , but this ends up quoting '-metadata' as well, so the command line argument is not properly recognized.  I guess I would like to only surround each argument with quotes if that argument was quoted to begin with. This seems difficult to do because bash strips the quotes before being passing arguments to the script/function.  Or is there a better method of relaying the -metadata arguments? (like environment variables or files)
48411
To create a playlist for all of the music in a folder, I am using the following command in bash:  ls &gt; list.txt   I would like to use the result of the pwd command for the name of the playlist.  Something like:  ls &gt; ${pwd}.txt   That doesn't work though - can anyone tell me what syntax I need to use to do something like this?  Edit: As mentioned in the comments pwd will end up giving an absolute path, so my playlist will end up being named .txt in some directory - d'oh! So I'll have to trim the path. Thanks for spotting that - I would probably have spent ages wondering where my files went!
91482
Duplicate:        How do I get the path of a the Python script I am running in? I was doing dirname(sys.argv[0]), however on Mac I only get the filename - not the full path as I do on Windows.  No matter where my application is launched from, I want to open files that are relative to my script file(s).
81295
Is there any easy way to pass (receive) named parameters to a shell script?  For example,   my_script -p_out '/some/path' -arg_1 '5'   And inside my_script.sh receive them as:  # I believe this notation does not work, but is there anything close to it? p_out=$ARGUMENTS['p_out'] arg1=$ARGUMENTS['arg_1']  printf "The Argument p_out is %s" "$p_out" printf "The Argument arg_1 is %s" "$arg1"   Is this possible in Bash or Zsh?
109572
Can I change effective process name of a Python script? I want to show a different name instead of the real name of the process when I get the system process list. In C I can set  strcpy(argv[0],"othername");   But in Python   argv[0] = "othername"   doesn't seem to work. When i get process list (with ps ax in my linux box) the real name doesn't change. I prefer a portable solution (or else one solution for posix and another for windows environments), if it exists.  Thanks in advance