I was facing this issue of passing the array from php to the bash file. To pass array to bash file write the following lines of code.
Php Code:
$array= $countries_array; // Countries array from database
shell_exec('sh get_countries.sh '.join(' ',$array));
If your php variable $array is a php Array, you need to convert it to a string to do the call. For Example:
shell_exec('sh get_countries.sh '.join(' ',$array));
Then your countries.sh
should look exactly like given below
#!/bin/bash
declare -a myArray
myArray=("$@")
for arg in "${myArray[@]}"; do
echo "$arg"
done
Make sure you have given permission to the file for execution with chmod a+rx countries.sh
.
Here countries data will be displayed in bash file.
0 Comment(s)