хорошо .. это сделка. у меня есть сценарий bash, подобный следующему. сценарий предназначен только для того, чтобы показать вам, что я имею в виду ... это может показаться странным ... но это именно то, что мне нужно:
#/bin/bash
runcommand () {
message="$1"
shift
echo "$message"
$@ > /tmp/logfile
if [ $? -gt 0 ]; then
cat /tmp/logfile
fi
}
runcommandwrapper () {
wrapperoptions="$1"
shift
$*
}
rm -f /tmp/test ; rm -f /tmp/logfile
runcommand "echo into file" echo "SUCCESS" > /tmp/test
echo "-----------------"
echo "test file:"
echo "-----------------"
cat /tmp/test
echo "-----------------"
echo
echo "-----------------"
echo "logfile file:"
echo "-----------------"
cat /tmp/logfile
echo "-----------------"
echo
echo
echo
rm -f /tmp/test ; rm -f /tmp/logfile
runcommand "echo into file" 'echo "SUCCESS" > /tmp/test'
echo "-----------------"
echo "test file:"
echo "-----------------"
cat /tmp/test
echo "-----------------"
echo
echo "-----------------"
echo "logfile file:"
echo "-----------------"
cat /tmp/logfile
echo "-----------------"
echo
это работает
runcommand "running command mount" mount
это не работает
runcommand "running command fdisk" fdisk > /tmp/fdiskoutput
в этом случае текст в кавычках не рассматривается как целый аргумент в сценарии оболочки. попробуйте, вы поймете, о чем я. -> РЕШЕНО
Итак, запуск приведенного выше скрипта возвращает:
-----------------
test file:
-----------------
echo into file
-----------------
-----------------
logfile file:
-----------------
SUCCESS
-----------------
echo into file
-----------------
test file:
-----------------
cat: /tmp/test: No such file or directory
-----------------
-----------------
logfile file:
-----------------
"SUCCESS" > /tmp/test
-----------------
но ожидаемый результат:
-----------------
test file:
-----------------
SUCCESS
-----------------
-----------------
logfile file:
-----------------
-----------------
echo into file
-----------------
test file:
-----------------
SUCCESS
-----------------
-----------------
logfile file:
-----------------
-----------------
как я могу передавать команды с перенаправлением или подкладкой каналов в качестве команды другой функции в bash?
и помощь и советы были бы очень признательны! Я понятия не имею, как заставить это работать, и возможно ли это вообще?
* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a
single word with the value of each parameter separated by the
first character of the IFS special variable. That is, "$*" is
equivalent to "$1c$2c...", where c is the first character of the
value of the IFS variable. <snip>
@ Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$@" is equivalent to "$1"
"$2" ... <snip>
Поэтому вам нужно использовать "$@"
(включая двойные кавычки) вместо $*
поскольку вы хотите сохранить исходное цитирование параметров.