I have been researching over the internet
like How to pass parameter to inside python code which is embedded within Unix function
Overview :
function_name()
{
python -c << END
...... python code .....
...... python code .....
...... python code .....
END
}
Below is the way I call above Unix function into my main.sh
script and execute it
. /d/demo/function.sh
echo "Function started"
function_name 10 5
echo "Function ended"
If parameter passed to function was a digit[number]
, then I was successful in passing parameter to python script like below and got expected output : 15
function_name()
{
get_value1="$1"
get_value2="$2" python -c << END
import os
firstparameter=int(os.environ['getvalue1'])
secondparameter=int(os.environ['getvalue2'])
C=firstparameter + secondparameter
print (C)
END
}
But if i want to pass string like path ='/d/demo/ABC'
, How to do ?
concatenate_ABC()
{
a="$1"
b="$2"
c="$3"
python -c << END
import os
get_path=int(os.environ['a'])
get_filename=int(os.environ['b'])
get_nm=int(os.environ['c'])
with open (get_path+'/'+get_filename,r) as f:
fname=get_path +'/' + get_nm
print (fname)
END
}
Below is the way I call above Unix function into my main.sh
script and trying to execute not able to achieve
. /d/demo/function.sh
echo "Function started"
concatenate_ABC "/d/demo/ABC" "Dummy.txt" "XYZ"
echo "Function ended"
The Dummy.txt file as list of path name
/d/d1
/d/d2
/d/d3
/d/d4
So from function the parameter should be passed to python code like below
concatenate_ABC "/d/demo/ABC" "Dummy.txt" "XYZ"
The above 3 parameters should be used in code like below
get_path="/d/demo/ABC"
get_filename=Dummy.txt
get_nm=XYZ
with open (get_path + '/' + get_filename,r) as f:
fname=get_path +'/' + get_nm
print (fname)