Passing array argument to bash from php

2020-03-27 03:32发布

问题:

I am trying to pass array from shell_exec and want to access the array in bash file. I tried to research from many websites but did not find the proper solution for it. Please help me out to resolve this issue. Thanks in advance

Php Code

$array= $countries_array; // Countries array from database
shell_exec('sh get_countries.sh '.$array);

Bash File countries.sh

echo $1;

I have understanding of array in bash file:

#!/bin/bash
declare -a myArray
myArray=("$@") 
for arg in "${myArray[@]}"; do
 echo "$arg"
done

But I am confused, how to fetch $array in bash file?

Also for passing variable, use the following code:

$var=shell_exec('sh get_countries.sh hello');
echo $var;

Bash File==>

 echo $1

Output will be hello

echo 'sh countries.sh '.join(' ',$data);

output==>

sh countries.sh a940cfb9-2abe-5835-9d69-41c0d992c38d 768bd771-359f-5da0-9f66-ccae4460f98d 778f384c-8496-56f1-96e6-4a03b2c22ff5 45ba4726-bba8-5e7b-ab62-c0e30df2e81b c8eb2a47-dc44-5439-80db-b6c7b7c1d30c c875edce-2177-55c9-bfa0-c4af3b9ac281 21c6ea3f-b6f3-5fa6-b693-863dddbe22d3 944d2f91-e4a3-53e3-9379-0a4cb6819dd5 a3db4ca5-7442-529d-bbf2-aa0dc74dd3e3 c0698faa-8a83-5aea-8f41-e8d993ff7fb4 4d0ab432-6e40-5e5d-8304-9b2108cf6a0f 54522d73-15bd-5061-aaf3-beb7cc6a0ed2 4d4a7d51-7af4-538d-a09e-92335eecb7b4 be9406d5-26af-52d7-a141-1f8d299beb9b 89bb6275-5047-5f0f-a369-8e0c9028654c 24151e55-c1ba-5ca3-8728-2558faf308ea 13895b30-322f-5886-8eba-7272fb4f990f a31d7cc7-49ea-5746-be3e-b15fe3894d43 e5505a29-37c5-582a-872a-6a65d74c8ded 92c4a93d-c91c-55eb-8f9f-fcb675d27cee 20c7086e-1599-52e6-8d8e-287b9e0cb58b 48a2bc90-14e0-575d-a0de-31140eb885ac 2d3772ca-9a65-5707-ad22-f53076acc3c3 731863e8-6889-5691-ad6e-4fd34c747889 762244b1-f952-5a34-8f71-eb2d856be293 ddb0eb7e-4ee5-56b7-a65a-d8dcf8238618 ff861dba-13f3-5282-afc2-9b9d7de3c18a eeb07bf4-a752-5289-ae47-3d549c54a552 c40bb588-4db7-5602-a12c-2697a9bf671a b89c6a26-87e0-5deb-ba28-f9cc39d7aeeb 11989abf-c6c0-5593-8451-e199e1a54c16 c9b19a1f-005c-5d0e-b7e1-5f52697cf4cf cfa46bd6-ac53-5d61-be9d-ec0d949138dc fe584292-01ab-5041-8c34-c824b8e4c5a7 2230ef16-921c-5fe1-ba69-79cf7c5ecff3 a1834c7f-bb69-59a4-b596-633590314bf6 7a41586b-927a-5100-9b9e-bff61e47440e e9a04859-a739-5b19-beda-b1d715f0b5da 2ba8a83a-92d0-5e18-81fa-1c09bcb4f1e2 4530e663-b036-5f33-8ad5-78dbf672f1cc 67ce40ed-f6c5-52a0-8eed-0b1c351765cb e6081884-fc3e-5cd5-8fdf-b4256aef251c 21668da0-a24e-5f74-ae14-32111c3ee78b 1a7f1847-87ad-5255-8011-177535b5979d e8d56819-7de5-567c-8672-55ea33c90676 cdc1c35a-2455-57e4-a8e5-4e01fd4f2d07 3f137a13-7011-5306-95e0-d713df89f9e6 e2153d20-e940-5c15-892c-a26e41afb9ca eb0e5967-3e1d-5eef-a453-0c74eb9af66a 0de046fc-40a6-599a-884f-18efb8281dc8 769bba0e-4b25-55e9-95c2-7e32096a05e7 e3cc0250-61bb-55c5-b3d4-90f3fb8a3290 8a9beccd-86d9-5150-b4ab-624e007c3594 91c72559-f535-5067-9989-9e0095f97ce3 41147580-3444-5ace-9e67-1f0f060474fd b1cda433-3059-5240-b59b-91694336fc35 7d042de8-ed7a-5faf-9a16-2ae409e5a2f8 f116b2b7-490d-5451-abc3-1141868da92e 1f19bb92-23fc-5f2e-ac5e-bbc3f9965aeb

回答1:

If your php variable $array is a php Array, you need to convert it to a string to do the call, eg:

 shell_exec('sh get_countries.sh '.join(' ',$array));

Then your countries.sh should be exactly what you gave as an example:

#!/bin/bash
declare -a myArray
myArray=("$@") 
for arg in "${myArray[@]}"; do
 echo "$arg"
done

Make sure you permit the file for execution with chmod a+rx countries.sh.



标签: php bash shell