"$#" in shell separates arguments with quotes even though it shouldn't [duplicate] - python

This question already has answers here:
Bash: space in variable value later used as parameter
(4 answers)
How can the arguments to bash script be copied for separate processing?
(2 answers)
Closed 5 years ago.
I'm trying to parse arguments in a shell script for a python program. We're using a wrapper who takes the command line arguments and calls the correct python module.
For some reason, it keeps splitting up args I'm passing with double quotes. This is how I get the args:
args="$#"
Then I run:
runner.sh --host="XHOST 1"
or
runner.sh --host "XHOST 1"
the "$#" breaks the "XHOST 1" into 2 separate tokens of XHOST and 1.
First I've tried using the "$*" as always, but it didn't work. Now I'm using $# and it keeps splitting the args. This was tested on a rhel-7.2 machine.
Is there another way to parse shell args to try and keep them as one token when they are wrapped in quotes? Am I missing something here?

Related

Is it possible run a string in Python [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 1 year ago.
Is it possible to run a String text?
Example:
str = "print(2+4)"
Something(str)
Output:
6
Basically turning a string into code, and then running it.
Use exec as it can dynamically execute code of python programs.
strr = "print(2+4)"
exec(strr)
>> 6
I will not recommend you to use exec because:
When you give your users the liberty to execute any piece of code with the Python exec() function, you give them a way to bend the rules.
What if you have access to the os module in your session and they borrow a command from that to run? Say you have imported os in your code.
Sure is, looks like your "Something" should be exec.
str = "print(2+4)"
exec(str)
Check out this previous question for more info:
How do I execute a string containing Python code in Python?

How to get exact match while writing dataframe in file in pyspark using escape or quote? [duplicate]

This question already has answers here:
Propagate all arguments in a Bash shell script
(12 answers)
Closed 2 years ago.
I am trying to load Data frame into file but not able to get exact match. Can you please help me on this?
example:
"From...............\"dawood\"...........\"oral use\"........"
but i am getting:
"From................\"dawood\"...........\\"oral use\\\"......"
i am using below code to write the dataframe:
df.repartition(1).write.format('com.databricks.spark.csv').mode('overwrite').save(output_path,quote='"', sep='|',header='True',nullValue=None)
Can you please help me how to get exact match for all the reords.
either just copy this inside your shell script:
python imed_consump.py 'Smart Source'
but then your parameter is always fixed. should this not be desired, then do following inside shell
python imed_consump.py "$1"
and execute your shell like:
bash imed_consump.sh 'Smart Source'

Need to run shell command from Python script and store in a variable [duplicate]

This question already has answers here:
Store output of subprocess.Popen call in a string [duplicate]
(15 answers)
Closed 3 years ago.
I have to run a shell command from python and get the output of that command into a python variable
python_var = subprocess.check_output('/opt/PPPP/QQQ/my_cmd -option1 -option2 /opt/XXXX/YYYY/ZZZZZ/my_file')
You need to hand-split the arguments into a sequence, not just pass a string (passing a whole string requires shell=True on Linux, but introduces all sorts of security/stability risks):
python_var = subprocess.check_output(['/opt/PPPP/QQQ/my_cmd', '-option1', '-option2', '/opt/XXXX/YYYY/ZZZZZ/my_file'])

Passing a variable to a subprocess call [duplicate]

This question already has answers here:
Why does passing variables to subprocess.Popen not work despite passing a list of arguments?
(5 answers)
Closed 1 year ago.
I'm trying to cut the first n lines of a file off a file, I calculate n and assign that value to myvariable
command = "tail -n +"myvariable" test.txt >> testmod.txt"
call(command)
I've imported call from subprocess. But I get a syntax error at myvariable.
There are two things wrong here:
Your string concatenation syntax is all wrong; that's not valid Python. You probably wanted to use something like:
command = "tail -n " + str(myvariable) + " test.txt >> testmod.txt"
where I assume that myvariable is an integer, not a string already.
Using string formatting would be more readable here:
command = "tail -n {} test.txt >> testmod.txt".format(myvariable)
where the str.format() method will replace {} with the string version of myvariable for you.
You need to tell subprocess.call() to run that command through the shell, because you are using >>, a shell-only feature:
call(command, shell=True)

passing more than one variables to os.system in python [duplicate]

This question already has answers here:
How to use python variable in os.system? [duplicate]
(2 answers)
Closed 1 year ago.
I want to pass two variables to the os.system() for example listing files in different format in specific directory like (ls -l testdirectory) in which both a switch and test directory are variable.
I know for single variable this one works:
option=l
os.sytem('ls -%s' option)
but I dont know how to pass two variables?
you are asking about string formating (since os.system takes a string, not a list of arguments)
cmd = "ls -{0} -{1}".format(var1,var2)
#or cmd = "{0} -{1} -{2}".format("ls","l","a")
os.system(cmd)
or
cmd = "ls -%s -%s"%(var1,var2)
or
cmd = "ls -"+var1+" -"+var2
This, for example, works:
os.system('%s %s' % ('ls', '-l'))

Categories