문제

I am trying to run the following script but the value of the variable $user does not get printed. Can someone please explain?

#!/usr/bin/ksh -x

x=`tty`
at -k now <<end
        user=Bill
        echo $user >> $x
end
도움이 되었습니까?

해결책

You cannot set a variable inside heredoc. You can do this:

x=`tty`
user=Bill

at -k now <<end
   echo $user >> $x
end

다른 팁

The problem is that variables are expanded in here docs when the end token is unquoted.

This means that the command that you pass to at is:

user=Bill
echo >> /dev/pts/4

If you quote the end token, i.e.

at -k now << 'end'
        user=Bill
        echo $user >> $x
end

then what you're passing to at is

user=Bill
echo $user >> $x

In this case $user is set in and expanded by at's invoked shell, and will expand as you expected.

Note in this case that $x has to be exported, since otherwise at won't see it, won't include it in the environment of the invoked shell.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top