This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 3 years ago.
I want to run a python script which is parsed from a bash file. When I try to run it, I get errors everywhere and I do not know what the issue could be. Any help?
The bash script (run_dmd_atomic.sh) looks like this:
#!/bin/bash
BATCH_SIZE=${1}
CODE_LENGTH=${2}
EPOCHS=${3}
SP=${4}
A1=${5}
A2=${6}
A3=${7}
srun python3 dmd_solver.py \
--batch_size ${BATCH_SIZE} \
--code_length ${CODE_LENGTH} \
--epochs ${EPOCHS} \
--sp ${SP} \
--a1 ${A1} \
--a2 ${A2} \
--a3 ${A3} \
The error I get is:
[tmarta#eu-login-09-ng training]$ ./run_dmd_atomic.sh
./run_dmd_atomic.sh: line 20: srun: command not found
In bash script, no white space is allowd in variable assignment expression. Otherwise, bash interpret it as a command or excutable.
Your error log is pointing out that.
For example, in your script
BATCH_SIZE = ${1}
should be
BATCH_SIZE=${1}
Related
I'm trying to convert over a Bash script that includes the following commands:
PYCODE=$(cat << EOF
#INSERT_PYTHON_CODE_HERE
EOF
)
RESPONSE=$(COLUMNS=999 /usr/bin/env python3 -c "$PYCODE" $#)
The idea being that a sed find/replace is then used to inject an arbitrary Python script where #INSERT_PYTHON_CODE_HERE is, creating the script that is then ran.
The corresponding Fish command would seem to be something like this
set PYCODE "
#INSERT_PYTHON_CODE_HERE
"
set RESPONSE (COLUMNS=999 /usr/bin/env python3 -c "$PYCODE" $argv)
but this falls apart when you have a Python script that can include both ' and " (and any other valid) characters.
What is the correct way to handle translate this use of EOF?
As a side note, I would prefer not to modify the sed command that is injecting the python code, but for reference here it is:
set FISH_SCRIPT (sed -e "/#INSERT_PYTHON_CODE_HERE/r $BASE_DIR/test_sh.py" $BASE_DIR/../src/mfa.fish)
This question already has answers here:
Using subprocess.run with arguments containing quotes
(3 answers)
Closed 2 years ago.
I'm trying to convert my kvm build script written in BASH to Python. In BASH, I've written the following lines within my script to execute a fresh KVM instance built from an ISO image:
virt-install \
--name=${VMNAME} \
--ram=${MEMSIZE} \
--vcpus=${VCPUS} \
--os-type "linux" \
--location ${ISOFILE} \
--file=/var/lib/libvirt/images/${VMNAME}.dsk \
--file-size=${DISKSIZE} \
--network bridge=br0 \
--graphics=none \
--os-variant="rhel7" \
-x 'console=ttyS0,115200n8 serial' \
-x "ks=http://192.168.1.10/boot/centos7.ks"
This creates a successful build, and is visible through the "virsh" console using virt-install.
The script I'm using in Python to mimic what is happening in my BASH script looks like this:
kvm_cmd = ['/usr/bin/virt-install', \
'--name=' + SELECT_SPECS[0], \
'--ram=' + SELECT_SPECS[2], \
'--vcpus=' + SELECT_SPECS[3], \
'--os-type=linux', \
'--location=' + CONFIG_VM[1], \
'--file=/var/lib/libvirt/images/' + SELECT_SPECS[0] + '.dsk', \
'--file-size=' + SELECT_SPECS[4], \
'--network=bridge:br0', \
'--nographics', \
'--os-variant=' + CONFIG_VM[2], \
'-x "console=ttyS0,115200n8 serial"', \
'-x "ks=' + KS_REPO + CONFIG_VM[0] + '.ks"']
process = subprocess.run(kvm_cmd, stdout=subprocess.PIPE)
print(kvm_cmd)
print('VM ' + SELECT_SPECS[0] + ' created!')
return
Right now, I'm trying to perform the BASH command using Python's subprocess.run() function. Although I'm not seeing the serial output during run time, I can send a ^] signal while the Python script is running to exit out of the console, and re-enter the virsh console in another window/terminal.
Entering console, I can see that the installation runs into some anaconda issues that report "Kickstart file /run/install/ks.cfg is missing".
I can confirm that all my command argument variables are where they belong. I've used both --network bridge:br0 and --bridge=br0 for testing, as well as defining a kickstart file locally with '-x "ks=file:/centos7.ks' and --initrd-inject=/path/to/centos7.ks with the same results.
['/usr/bin/virt-install', '--name=test', '--ram=2048', '--vcpus=1', '--os-type=linux', \
'--location=/DataStore/ISOs/CentOS-7-x86_64-Minimal-1708.iso', '--file=/var/lib/libvirt/images/test.dsk', \
'--file-size=20', '--bridge=br0', '--graphics=none', '--os-variant=rhel7', '-x \
"console=ttyS0,115200n8 serial"', '-x "ks=http://192.168.1.10/boot/centos7.ks"']
Can anyone help me figure out why the kickstart won't load using python's Subprocess.run() function, but I can run it no problem when I run the command in BASH?
Is there a better way to create my kvm's with the details mentioned other than using BASH virt-install in a subprocess, preferably with a Python library? I've had a look at libvirt, but could not find a solution to my BASH query.
The equivalent to the shell script fragment:
-x 'console=ttyS0,115200n8 serial' \
-x "ks=http://${KS_REPO}/boot/${CONFIG_VM}.ks"
is (assuming a new enough Python to support f-strings for brevity):
[
'-x', 'console=ttyS0,115200n8 serial',
'-x', f'ks=http://{KS_REPO}/boot/{CONFIG_VM[0]}.ks',
]
No literal quotes, only syntactic ones, in both languages.
I have written a small python programme which writes a C-shell script, and then calls the script with subprocess. However, python fails to execute the script. I tested this in the terminal and discovered that the script will also not run from the terminal (i.e. not a python issue). Further testing revealed I could write an identical script in a text editor (vi) and then successfully execute it from the terminal (i.e. I don't think the contents of the script are at fault). Eventually I found that if I tried to run the output from python directly in the terminal with ./myscript.com it would not run. However, if I open with vi, make NO changes, save and then run from the terminal using ./myscript.com (i.e. the exact same command) it will execute. The script is included below, in case it is of use, however, most of the contents are very specific to the particular programme I am trying to run. I have no idea how to debug this further, or why the effect of opening, saving and closing in vi is sufficient to allow the script to be executed. (I have checked the permissions for example and these are unchanged after the vi step.) Any suggestions on how to proceed which be much appreciated.
#!/bin/csh
#
bin2pipe -in /full/path/to/this/script/recon.spc -bad 0.0 -noswap \
-xN 1714 \
-xT 1714 \
-xFT Freq \
-xMODE Real \
-xSW 4184.570312 \
-xCAR 10.929523 \
-xOBS 800.130005 \
-xLAB 1H \
-yN 2048 \
-yT 2048 \
-yFT Freq \
-yMODE Real \
-ySW 1700.680054 \
-yCAR 125.728023 \
-yOBS 81.075996 \
-yLAB 1H \
-ndim 2 \
|nmrPipe -fn TP -auto \
|nmrPipe -fn REV \
|nmrPipe -fn TP -auto \
-out recon.spc.pipe.ft2 -ov -verb
In case this is useful I used xxd (as suggested in the comments above, http://linuxcommand.org/man_pages/xxd1.html) to convert both script files, one created in python and one written manually, to hex
xxd script.txt output.txt
I then compared the output for the two files with diff and could see an additional character 0a in hex at the end of the file. This corresponds to a newline. As detailed here Why should text files end with a newline? and here VIM Disable Automatic Newline At End Of File, vi adds a newline character at the end of the file if there isn't one. This was enabling the script to run. Without the newline character at the end (which I hadn't included in the python script), the script wouldn't run.
This question already has answers here:
Why does cURL return error "(23) Failed writing body"?
(19 answers)
Closed 6 years ago.
I'm not famililar with the shell but i have to run this command from a Python file:
curl -XDELETE 'localhost:9200/event/_query?pretty' -d '{"query" : { "range" : { "int_timestamp" : { "lte" : "2016-06-09 08:55:10" } } } }'
From the file i use os.popen to execute the command. I have tried it also with os.system. In both cases it gives me the following Error:
(23) Failed writing body
But when i run this command in the shell, it worked perfect.
What did i wrong?
Thanks for help!
EDIT:
I don't know why because i doesn't change something. But now it worked with os.system instead of os.popen.
Anyway thanks for help!
The error (23) Failed writing body indicates you didn't properly wait for curl to output its data before closing the file descriptor. This can be caused by not calling read() after os.popen()
os.system can do the trick, but it doesn't seem like you can retrieve its standard output.
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 7 years ago.
I try to check file existence on a remote server. Everything works fine for me, except that I want to pass the file path as a variable.
Here is what I do
_,stdout,_=ssh.exec_command("[ -f d:/tryssh/1.txt ] && echo OK")
and I need this d:/tryssh/1.txt to be a variable that I specify in the python script to be used later in the bash, something like this
`_,stdout,_=ssh.exec_command("[ -f $filePath ] && echo OK")`
As your command is a string, use it as a string:
file_path = "d:/tryssh/1.txt"
command = "[ -f %s ] && echo OK" % file_path
_,stdout,_=ssh.exec_command(command)