Hot answers tagged

39

To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script) that you create in the script, as in #!/bin/bash -e # Create script as "script.py" cat >script.py <<'END_SCRIPT' print("TESTPRINT") END_SCRIPT # Run script.py python script.py rm script.py (this ...


17

In addition to Kusalananda's answer, if you want the entire script to be run by python you can just change the first line to #!/usr/bin/env python3 and run it like any normal shell script. That way you don't have to remember what script you have to run with which interpreter.


7

You may also try the <<< (Here Strings) operator to save on lines: $ python <<< 'print("MyTest")' MyTest


4

$ sort file.txt \ | grep -f <(cut -d' ' -f1 file.txt | sort | uniq -d) \ | awk 'last && last != $1 { printf "\n" }; { last=$1 ; print}' 542ed609dfc4d0cae44c4b7be6d66382 mba.txt 542ed609dfc4d0cae44c4b7be6d66382 tyrion final.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 foo.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 imac.txt (Thanks to "cas" for the awk ...


3

On RHEL and Fedora systems, the kernel package is special in that there is a /usr/lib/kernel/install.d and /etc/kernel/install.d that have scripts for running during kernel installs, and /usr/lib/kernel/postinst.d and /etc/kernel/postinst.d for after install. Other packages, such as akmods or dkms can drop scripts in the appropriate directory to invoke ...


3

The file-system where the script resides was mounted with 'NOEXEC' flag /dev/mapper/systemvg-home on /home type ext4 (rw,noexec,nosuid,nodev)


3

The Python script prints three lines with text. To read these into an array in bash, use readarray: readarray -t chose < <( python3 test.py ) printf '> %s\n' "${chose[@]}" The readarray -t command will read lines into the given array from standard input. Standard input is redirected from your Python script using a process substitution. The ...


3

Looks like declare -a choice=$( python3 test.py ) making a 1 length array where choice[0]='first second third' Try declare -a choice=( $( python3 test.py ) )


3

With sort and uniq sort file.txt | uniq -w 32 --all-repeated=separate 542ed609dfc4d0cae44c4b7be6d66382 mba.txt 542ed609dfc4d0cae44c4b7be6d66382 tyrion final.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 foo.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 imac.txt -w 32 limits uniq comparison to the MD5 hash (so it considers equals lines with same MD5) --all-repeated=...


2

With a perl Hash of Arrays: $ perl -alne ' push @{ $h{$F[0]} }, $_ }{ for $k (sort keys %h) { @a = @{ $h{$k} }; print join "\n", @a, "" if $#a > 0 } ' file.txt 542ed609dfc4d0cae44c4b7be6d66382 mba.txt 542ed609dfc4d0cae44c4b7be6d66382 tyrion final.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 foo.txt ...


2

Your guess is correct that 0 is the return code from your shell command. os.system(cmd) is executing your command and outputting the result, and then the return code is stored in event_id and printed. You should use the subprocess module instead. import subprocess cmd = "/path/to/ctl so list | tail -1 | awk '{print $1}'" event_id = subprocess.check_output(...


2

Just for variety, here's a solution using mostly bash and bc for floating arithmetic. #!/usr/bin/env bash # r is an associative array of weights, indexed by column name declare -A r source <(awk '{ print "r[\"" $1 "\"]=" $6}' <( tail +2 file2)) hdr= while read line do if ! [ $hdr ] then hdr=($line) ...


1

There are many problems in your script. and this is not the place for python question i think. but i'm gone answer your question. import argparse if __name__ == '__main__': parser = argparse.ArgumentParser(description='Description of your program') parser.add_argument('-p','--port', help='Port_number', required=True) args = vars(parser....


1

You've asked if there is any way to connect to oracle without "cx_oracle" and without installing Instant client The documentation referenced from the error message clearly states ODPI-C requires Oracle Client libraries, which are found in Oracle Instant Client, or an Oracle Database installation, or in a full Oracle Client installation. If you haven'...


1

You can also set python path while calling the python file from bash script: export PYTHONPATH=/tmp/python_dep.zip && python test_my.py


1

A file that's 100x100 isn't large so it doesn't need any kind of special treatment. I had been picturing generating different output files per column and then using paste to combine them but with a file that small it's not necessary, just storing the values in arrays is fine: $ cat tst.awk BEGIN { OFS = "\t" } NR==FNR { key2mult[$1] = $NF next } FNR=...


1

The problem solved. The main reason was that when I tried to compile the python 3.7.4 on my laptop, I had set the installation destination to be /usr/local rather /usr. Recompiled and installed with the second address and the problem solved.


1

As you can see in your question “/usr/bin/python3 -> python3.6”, python3 is a symlink to python3.6. It uses next to no space. Python3 will be a link to the latest python3.x than you have installed. You could remove the link, but don't you will break something. It may appear that you have two pythons installed /usr/bin/python3.6 and /usr/bin/python3.6m. ...


1

Having python3 and python3.6 in the /usr/bin directory doesn't mean that you have two installations of Python. Usually actual python interpreter is installed in /usr/bin/pythonX.Y where X in major version and Y is minor version. In your case you actually have two versions of Python3.6 installed: /usr/bin/python3.6 and /usr/bin/python3.6m. /usr/bin/python3 ...


Only top voted, non community-wiki answers of a minimum length are eligible