I'm beginning with bash and I'm executing a script :
$ ./readtext.sh ./InputFiles/applications.txt
Here is my readtext.sh code :
#!/bin/bash
filename="$1"
counter=1
while IFS=: true; do
line=''
read -r line
if [ -z "$line" ]; then
break
fi
echo "$line"
python3 ./download.py \
-c ./credentials.json \
--blobs \
"$line"
done < "$filename"
I want to print the string ("./InputFiles/applications.txt") in a python file, I used sys.argv[1]
but this line gives me -c. How can I get this string ? Thank you
It is easier for you to pass the parameter "$1" to the
internal
commandpython3
.If you don't want to do that, you can still get the
external
command line parameter with the trick of/proc
, for example:Note:
parent.sh
is important, or you should execute./parent.sh
withbash
, else you will get no command line param inps
or/proc/$PPID/cmdline
.[2:-1]
:ext.split('\0') = ['bash', './parent.sh', 'aaaa', 'bbbb', '']
, real parameter of./parent.sh
begins at 2, ends at -1.Update: Thanks to the command of @cdarke that "
/proc
is not portable", I am not sure if this way of getting command line works more portable:which still have the same output.
This is the python file that you can use in ur case
How to use sys.argv
How to use join method inside my python code