Ответ 1
Введите ваши аргументы в Testscript 1:
echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 "$1" "$2"
echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 $1 $2
TestScript2 выглядит так:
echo "TestScript2 Arguments received from TestScript1:"
echo "$1"
echo "$2"
echo "$#"
Когда я выполняю testcript1 следующим образом:
./testscript1 "Firstname Lastname" [email protected]
Желаемый результат должен быть:
TestScript1 Arguments:
Firstname Lastname
[email protected]
2
TestScript2 Arguments received from TestScript1:
Firstname Lastname
[email protected]
2
Но фактический результат:
TestScript1 Arguments:
Firstname Lastname
[email protected]
2
TestScript2 Arguments received from TestScript1:
Firstname
Lastname
3
Как решить эту проблему? Я хочу получить желаемый результат вместо фактического вывода.
Введите ваши аргументы в Testscript 1:
echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 "$1" "$2"
Вам нужно использовать: "[email protected]"
(с кавычками) или "${@}"
(то же самое, но также сообщая оболочке, где имя переменной начинается и заканчивается).
(и НЕ используйте: [email protected]
, или "$*"
, или $*
).
Пример:
#testscript1:
echo "TestScript1 Arguments:"
for an_arg in "[email protected]" ; do
echo "${an_arg}"
done
echo "nb of args: $#"
./testscript2 "[email protected]" #invokes testscript2 with the same arguments we received
Я не уверен, что понял другое ваше требование (вы хотите вызывать "./testscript2" в одинарных кавычках?), так что вот два диких предположения (изменение последней строки выше):
'./testscript2' "[email protected]" #only makes sense if "/path/to/testscript2" containes spaces?
./testscript2 '"some thing" "another"' "$var" "$var2" #3 args to testscript2
Пожалуйста, дайте мне то, что вы пытаетесь сделать
edit: после его комментария, говорящего, что он пытается использовать tesscript1 "$ 1" "$ 2" "$ 3" "$ 4" "$ 5" "$ 6" для запуска: salt 'remote host' cmd.run './testscript2 $1 $2 $3 $4 $5 $6'
У вас есть много уровней промежуточного уровня: testscript1 на хосте 1, нужно запустить "соль" и дать ему строку, запускающую "testscrit2" с аргументами в кавычках...
Возможно, вы можете "упростить", имея:
#testscript1
#we receive args, we generate a custom script simulating 'testscript2 "[email protected]"'
theargs="'$1'"
shift
for i in "[email protected]" ; do
theargs="${theargs} '$i'"
done
salt 'remote host' cmd.run "./testscript2 ${theargs}"
если THAt не работает, вместо того, чтобы запускать "testscript2 ${theargs}", замените LAST LINE выше на
echo "./testscript2 ${theargs}" >/tmp/runtestscript2.$$ #generate custom script locally ($$ is current pid in bash/sh/...)
scp /tmp/runtestscript2.$$ [email protected]:/tmp/runtestscript2.$$ #copy it to remotehost
salt 'remotehost' cmd.run "./runtestscript2.$$" #the args are inside the custom script!
ssh [email protected] "rm /tmp/runtestscript2.$$" #delete the remote one
rm /tmp/runtestscript2.$$ #and the local one
Я нашел следующие программы для меня
test1.sh
a=xxx
test2.sh $a
в test2.sh вы используете $1
для ссылки на переменную a
в test1.sh
echo $1
Выходной сигнал будет xxx