LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ambiguous redirect (https://www.linuxquestions.org/questions/linux-newbie-8/ambiguous-redirect-4175522301/)

anand1 10-16-2014 05:28 AM

ambiguous redirect
 
Hi Friends,

I am a beginner to shell script.I have run the shell script in tcsh shell.

In my shell script code,I am trying to redirect output to a file.
But getting ambiguous redirect

Code:

su -c "exit" - prime310 > pnoutput.txt 2>&1
2>&1 redirect stdout and stderr to pnoutput.txt.(correct me if i am wrong)

I also tried in bash shell. but same ambiguous redirect error came.

I have searched in Google. but not able to
find clear solution.

Please help me to resolve this issue.


Regards,
Anand

pan64 10-16-2014 06:14 AM

would be nice to see more, that looks not enough to find the reason.
please use [code]here comes your code[/code] to keep formatting.
You can put set -xv at the beginning of your script to see what's happening

mithun119 10-17-2014 04:55 AM

Hi Anand,

TCSH stdout/stderr redirection happens in a different way.

BASH:
[root@vmtest02 lab]# su -c "date" kummit01 > /tmp/mitx 2>&1
[root@vmtest02 lab]# cat /tmp/mitx
Fri Oct 17 04:44:51 CDT 2014


TCSH:
STDOUT redirection:
[root@vmtest02 lab]# su -c "date" kummit01 >& /tmp/mitx
[root@vmtest02 lab]# cat /tmp/mitx
Fri Oct 17 04:52:12 CDT 2014

STDERR redirection:
[root@vmtest02 lab]# su -c "dat" kummit01 > & /tmp/mitx
[root@vmtest02 lab]# cat /tmp/mitx
bash: dat: command not found
[root@vmtest02 lab]#

anand1 10-27-2014 07:42 AM

Hi Mithun,

Thanks for the reply.

After your suggestion, I have modified the code as below in the sh file.(I am using TCSH shell).

<code>
su -c "exit" - prime310 >& /export/home/prime310/pnoutput.txt
cat pnoutput.txt
</code>

and ran the sh file as
sh filename.sh
i.e
<code>
sh pn_failure_detection.sh
</code>

but sh file is not execute successfully and the shell got struck. I forcefully stop the shell buy pressing ctrl+x.

mithun119 10-27-2014 07:58 AM

This part of your command is perfectly fine. It works as desired on bash or tcsh.
su -c "exit" - prime310 >& /export/home/prime310/pnoutput.txt && cat pnoutput.txt --> in ideal case is not expected to give any output if the id exists

See eg. below for stdout and stderr redirected to the output file.
[root@vmtest02 lab]# su -c "exit" - test1 >& /tmp/test.^Ct
[root@vmtest02 lab]# cat /tmp/test.out
[root@vmtest02 lab]# su -c "exit" - test123 >& /tmp/test.out
[root@vmtest02 lab]# cat /tmp/test.out
su: user test123 does not exist


What else is going into your <code> sections needs to be scrutinized. What exactly are you trying to achieve. We require more info to tell you why your script slipped into loops.


All times are GMT -5. The time now is 01:40 AM.