简体   繁体   中英

pass php variables in a bash script

I want to run this script bash

#!/bin/bash
# file name: myscript.sh
PROJECT_DIR=$1
mkdir $PROJECT_DIR
mkdir $PROJECT_DIR/PolySkills
mkdir $PROJECT_DIR/PolySkills/cr
mkdir $PROJECT_DIR/PolySkills/cr/corrections
mkdir $PROJECT_DIR/PolySkills/cr/corrections/jpg
mkdir $PROJECT_DIR/PolySkills/cr/corrections/pdf
mkdir $PROJECT_DIR/PolySkills/cr/diagnostic
mkdir $PROJECT_DIR/PolySkills/cr/zooms
mkdir $PROJECT_DIR/PolySkills/data
mkdir $PROJECT_DIR/PolySkills/exports
mkdir $PROJECT_DIR/PolySkills/scans
mkdir $PROJECT_DIR/PolySkills/copies
cd $PROJECT_DIR/PolySkills
cp ~/file.tex $PROJECT_DIR/PolySkills

For the variable "PROJECT_DIR" that represents a path in which I will create folders, I want to retrieve its value from a php variable. I looked at some examples on the internet and I tried one but it does not work. This is what i used :

   chdir('~/');
   $pathtofile = "~/ExportEval/".$NomUE."/".$NomOcc."/".$Numtudiant;
   $directory=$pathtofile."/AMC_Project";
   $output = exec("./myscript $directory");

Knowing that the script file "myscript " exists in home "~ /"

thank you for your help :)

I update my question : I found what is the problem but I don't see what the solution as the variable $NomUE is composed of a sentence separated by spaces it considers that $ 1 is only the first of this sentence if I change $ 1 by $ 2 it takes the second word of that same sentence! I don't understand why it does not take $pathtofile as path !

You need to always escape variables before sending them to the command line. Also, ~ has no meaning here and there's no reason to use chdir() . Just use a fully qualified pathname instead:

<?php
$pathtofile = "/home/someuser/ExportEval/$NomUE/$NomOcc/$Numtudiant";
$directory = escapeshellarg("$pathtofile/AMC_Project");
$output = exec("/home/someuser/myscript $directory");

You may have problems running a script in a home directory, and reading from that directory. Best is to put the script in a proper location such as /usr/local/bin/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM