Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pass array argument to bash file from php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 860
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: