I'm trying to bypass arg
to execute multiple commands with $addr
in a shell script,but not worked.
#1.sh
arg='google.com |cat 1.txt'
echo ${arg}
addr="`echo ${arg}| sed 's|[\]||g' | sed 's|%20| |g'`"
echo ${addr}
addr="ping -c 1 "$addr
echo ${addr}
$addr
./1.sh
google.com |cat 1.txt
google.com |cat 1.txt
ping -c 1 google.com |cat 1.txt
ping: 1.txt: Name or service not known
In bash, It worked.
ping google.com|cat 1.txt
import java.lang.Runtime;
import java.lang.Process;
......
when arg='google.com -p 1'
, it worked.
arg='google.com -p 1'
echo ${arg}
addr="`echo ${arg}| sed 's|[\]||g' | sed 's|%20| |g'`"
echo ${addr}
addr="ping -c 1 "$addr
echo ${addr}
$addr
# ./1.sh
google.com -p 1
google.com -p 1
ping -c 1 google.com -p 1
PATTERN: 0x01
PING google.com(tsa01s13-in-x0e.1e100.net (2404:6800:4012:4::200e)) 56 data bytes
I think the problem is addr="ping -c 1 "$addr
, What does "string"$arg exactly did, how to escape "string"$arg to excute command?