简体   繁体   中英

Execute bash script with params from Javascript

I have some Javascript code that is used to determine a param that has to be then forwarded to a bash script on the target device. The bash script then copies a new file across and does a reboot of the system. I am using AJAX to do the call to the bash script and the code is as follows:

var config = get_config(); // Simply returns an integer

var xml_http = get_xml_http_object();
xml_http.open("GET", bash_script_id + "?" + config, false);
xml_http.send(null); 

The bash script looks as follows

#!/bin/sh
CONFIG_NUMBER="$1"

cp /config_file$CONFIG_NUMBER /file_to_use

reboot

The script is correct and works if I use it from the command line directly on the machine and manually pass in a variable however when called from Javascript the system does the reboot but does not copy the file. I have omitted some debug code and exception code for instance checking that $1 is not empty but this does not seem to be the issue. Any help is very much appreciated.

也许尝试在复制和重新启动之间添加“同步”,以防文件系统无法正确刷新...

Try removing the reboot command and use this command instead of your line:

cp /config_file$CONFIG_NUMBER /file_to_use 2> error_copy.log

This will attempt to copy the file and if there are any errors it will output them to error_copy.log..

I am suspecting that the file/directory write privileges might not be configured correctly on your system. In other words the question is that if the user which copies the file has the right to do so..

Solved the issue. The bash script has to be updated to use $QUERY_STRING and not $1. This can then be parsed(if you pass more than one param) to get all the params needed

#!/bin/sh
CONFIG_NUMBER=$QUERY_STRING

cp /config_file$CONFIG_NUMBER /file_to_use

reboot

Note

To work with command line as well as from Javascript you would have to accommodate both $QUERY_STRING and $1 etc.

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