I tried doing this in python and found questions here and here that look relevant, but I couldn't get it working from those answers. I am looking for a way to automate a manual process where I first change the directory and folder from the command prompt as follows.
d:
then
cd vph
then from the command prompt I run the following:
krr filename.xdf -vph 1,129 -s "StartFrequency: " -StartFrequency -n
The last line causes a frequency to be displayed at the command prompt. How can I save to a text file the frequency the last line returns, and how can I automate all the steps above? These steps need to be automated so I can do the same for over a thousand files. I worry nobody reading this has the krr software I need to use. If necessary, you can substitute the last line for something that will run on any windows computer and return a number. The "double-quotes" in the last line might be tricky if the last line needs to be in "double quotes".
import subprocess
#list of commands to be executed can be given one after another and add && between the commands.
#Only when the first command execution is successful the next command will get executed
lst_cmd = "d && cd vph && krr filename.xdf -vph 1,129 -s 'StartFrequency: ' -StartFrequency -n"
out,err = subprocess.Popen(lst_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
print ("the output is {}".format(out))
Related
This question already has answers here:
Have bash script answer interactive prompts [duplicate]
(6 answers)
Closed 1 year ago.
suppose i wanted to make a bunch of files full of gibberish.
if i wanted to one file of gibberish, then encrypt it using ccrypt, i can do this:
$ echo "12 ddsd23" > randomfile.txt,
now using ccrypt:
$ ccrypt -e randomfile.txt
Enter encryption key:
Enter encryption key: (repeat)
as you can see i am prompted for input for the key.
i want to automate this and create a bunch of gibberish files.
script in python to produce random gibberish:
import random as rd
import string as st
alphs = st.ascii_letters
digits = st.digits
word = ""
while len(word) < 1000:
word += str(rd.choices(alphs))
word += str(rd.choices(digits))
print(word)
now running this from bash script, saving gibberish to file:
#!/bin/bash
count=1
while [ $count -le 100 ]
do
python3 /path/r.py > "file$count.txt"
ccrypt -e "file$count.txt"
((count=count+1))
done
problem, as you can see:
$ bash random.sh
Enter encryption key:
ccrypt does not have an option to provide passphrase as an argument.
Question: is there a way for the bash script to provide the passphrase when shell prompts for it?
i am aware this can be solved just by doing the encryption in python but just curious if something like this can be done with bash.
if it matters: there is an option for ccrypt to ask for just one prompt.
[Edited]
My original answer suggested to do:
printf "$PASSPHRASE\n$PASSPHRASE\n" | ccrypt -e "file$count.txt"
which is the generic solution that should work with many tools that expect some input passed to their STDIN; but it doesn't seem to work with ccrypt for whatever reason.
However, ccrypt also has options for providing the passphrase in different (non-interactive) ways:
$ ccrypt --help
...
-K, --key key give keyword on command line (unsafe)
-k, --keyfile file read keyword(s) as first line(s) from file
...
Here's an example using -K. Note that it is "unsafe" because if you execute this command in your interactive shell, or run your script with -x (to print each executed command), the passphrase may end up in ~/.bash_history or in some logs, respectively, so dump the passphrase to a file and use -k in case that's important.
#!/bin/bash
# read the passphrase, do not display it to screen
read -p "Please provide a passphrase:" -s PASSPHRASE
count=1
while [ $count -le 100 ]
do
python script.py > "file$count.txt"
ccrypt -e "file$count.txt" -K "$PASSPHRASE"
((count=count+1))
done
You need to use the yes command in your bash code. Basically this command will provide the inputs for a script (ie. ccrypt) whenever it needs it. Check here for more info.
I have a python program that I have parsed into the linux command line. It requires two inputs. The first input will always be the same and I want the second input to be anywhere from 1-1030.Is there a way I can get python to run that automatically in the command line and each time increase the second input by +1? I would like to do this so I do not have to manually type the second input 1030 times.
My command line looks something like this:
$ python ex_script.py -d (first input) -r (second input)
Just use for loops in bash.
for i in {1..1030}
do
python ex_script.py -d <the first input here> -r $i
done
The loop will iterate through the numbers 1 to 1030. Then, you could put any command-line instructions inside the loop and use the variable $i, passing it as an argument for the instruction.
You could put it into a .sh file and make it executable using chmod +x.
Then run it as an executable script from the terminal.
There are multiple possibilities to get inputs from the command line
using import sys and sys.argv[n] will give you the n-th command line input as a string.
using a library such as argparse
To avoid the second argument a very simple fix would be to write a counter into a file and make your program read the number in this file increase it by one override the file. You could also check in your program that the number is smaller than 1030.
I would write a short second script that runs the first script with the required arguments. Not sure if this is the best way, but it would be my first choice to use subprocess:
import subprocess
for i in range(1030):
subprocess.run(['python', 'ex_script', 'arg1', str(i)])
Note that subprocess is specified with values in a list []. These values work in my Win10 environment, but you can adapt them for your own environment.
I am redirecting the output of a Python script to a file.
It works fine if there is no error.
But if there is any error , I wish to capture the error in another file which is not happening now.
Below is the script which I have written.
#echo off
mode con cp select=65001
set dt=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
cd C:\API_DOC\softeon_project\script
python -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt
echo "after python path"
pause
exit
Any help would be appreciable.
The usage of dynamic environment variable DATE depends on Windows Region setting as defined for the currently used user account.
For example with getting Tue 12/26/2017 written into a command prompt window on running in same window echo %DATE% it is possible to use either
set "dt=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
or better
set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"
Both command lines use string substitutions to get environment variable dt defined with string 2017-12-26. The difference is that the first command line references the characters in date string from left while the second command line references them from right. Therefore the second command line works also with no abbreviated weekday at beginning.
The help output for command SET on running set /? in a command prompt window explains string substitutions as used here.
A region independent solution to get current local date in format yyyy-MM-dd would be:
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%I"
set "dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"
This variant is explained in detail in answer on Why does %date% produce a different result in batch file executed as scheduled task?
The disadvantage is that WMIC takes more than a second to output the local date and time which makes this solution much slower than the solution using dynamic environment variable DATE.
I suggest to use:
#echo off
rem Define encoding UTF-8 for console.
%SystemRoot%\System32\mode.com CON CP SELECT=65001
rem Get current local date in format yyyy-MM-dd.
set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"
rem Change the current directory independent on current drive.
cd /D C:\API_DOC\softeon_project\script
rem Execute Python interpreter and redirect standard output messages
rem to file log_%dt%.txt and error messages to logerr_%dt%.txt.
python.exe -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt
echo "After python path"
pause
There was a trailing space in Python command line which is removed in code above. See the answers on Why does ECHO command print some extra trailing space into the file? and
Why is no string output with 'echo %var%' after using 'set var = text' on command line? why a trailing space in a batch file could result in an unexpected output into a file or even unexpected behavior on execution of a batch file.
And python was extended with file extension .exe to avoid that by chance a file python.bat or python.cmd is found first by Windows command interpreter as in this case the next line would not be executed anymore because batch files must be called from within a batch file with command CALL to return to calling batch file on finishing execution of called batch file.
Read also the Microsoft article about Using command redirection operators for an explanation of >> and 2>>.
I am writing a Python script, in which I create then execute a shell file.
This shell file is composed of several lines, each line being a shell command (with some grep, sort, etc..).
Here is an example of one of these command lines from my_shell_file.sh :
nohup grep -A1 "pattern" myfile.fastq | grep -v "pattern2" | grep -v "^pattern3" | grep "^pattern4" | sort -T ./tmp | uniq -c > Result_directory/result_file.txt &
The only thing that changes between each line is the pattern4 and the name of the result_file.txt.
Well, my Python script creates my_shell_file.sh very well, it contains all the command lines I expected and without any syntax error. Problem comes at next step, when trying to run this shell file with :
os.system("sh ./my_shell_file.sh")
I expect one result_file.txt per command line that I wrote in the my_shell_file.sh. Problem is that not all the command lines from my_shell_file.sh are run.
There is always a few lines, the last ones, that are never run, I don't know why.
It seems very strange because 1) that means Python can run my_shell_file.sh, so why doesn't he run the last lines 2) I see no syntax difference between each line of my_shell_file.sh, so I don't know why it stops a few lines before the end, 3) the line where it stops can be different from one try to another...
Sometimes in my log files, I see messages like :
./my_shell_file.sh: line 63: Premature EOF while searching for the correspondant « " »
./my_shell_file.sh: line 64: Syntax error: Premature EOF
It looks like an « " » is not closed, buuuut... I see no syntax error... Plus if I just change the order of the lines in my_shell_file.sh, the issue can be resolved...
Whatsmore if I myself run the my_shell_file.sh created by Python,there is no problem, all command lines are run !
Does anyone have an idea about why the last lines are randomly skipped when the script is running via Python ?
Thank you in advance
Python version : 2.6.6
i need some help with this...
I have a program installed on my computer that i want to call to calculate some things and give me an output-file...
in Matlab the command "dos()" does the job giving me also the cmd screen output in matlab.
I need this to work in python but i am making something wrong.
data='file.csv -v'
db=' -d D:\directory\bla\something.db'
anw='"D:\Program Files\bla\path\to\anw.exe"' + db + ' -i' + data
"anw" output is this one:
>>> anw
'"D:\\Program Files\\bla\\path\\to\\anw.exe" -d D:\\directory\\bla\\something.db -i file.csv -v'
## without the "" it does not work either
import subprocess as sb
p= sb.Popen('cmd','/K', anw) ## '/C' does not work either
i get the following error message from cmd.exe inside the python shell
Windows cannot find "\"D:\Program Files\bla\path\to\anw.exe"" Make sure you typed the name correctly, and then try again.
this line runs when i make a bat. file out of it.
it runs in matlab via "dos(anw)" so what is wrong here?
ps: i have blanks in my command... could this be the problem? i do not know where the first "\" comes from in the cmd. exe error message
for now i created a bat. file with all the stuff cmx.de should do in the specific directory where the input file lies...
i just had to tell python to change directory with
import os
os.chdir("D:\working\directory")
os.system(r'D:\working\directory\commands.bat')
it works good and gives me the output of cmd directly in the python shell