Ambiguos Redirect Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ambiguos Redirect Error
# 1  
Old 05-26-2016
Ambiguos Redirect Error

I have the below lines of code.
Code:
ls found*.tmp | while IFS= read -r entry; do
APP_NAME=$(sed -n -e 's/^.*<name>//' -e 's|</name>.*$||'p $entry | sed 's/\(<name>\|<\/name>\)//g')
echo "APP_NAME="$APP_NAME>$APP_NAME"_Deploy.cfg"
done

I m getting the below error while running the above script.
Code:
bash-3.2$ ./test.sh
./test.sh: line 7: $APP_NAME"_Deploy.cfg": ambiguous redirect
./test.sh: line 7: $APP_NAME"_Deploy.cfg": ambiguous redirect
./test: line 7: $APP_NAME"_Deploy.cfg": ambiguous redirect
./test: line 7: $APP_NAME"_Deploy.cfg": ambiguous redirect

I tried this echo "APP_NAME="$APP_NAME>"{$APP_NAME}"_Deploy.cfg

This time i did not get the error but the .cfg files were not generated when they should.

Code:
uname -a SunOS mymac 5.10 Generic_150400-23 sun4v sparc sun4v

Can you please help fix this.



Moderator's Comments:
Mod Comment Please use code tags consistently and correctly (not quote tags)!

Last edited by RudiC; 05-26-2016 at 06:20 AM.. Reason: Added / corrected code tags.
# 2  
Old 05-26-2016
Your code snippet doesn't have 7 lines, so I'd be inclined to look for the error somewhere else! Why don't you post the entire script?

In line 3 of your snippet, you seem to want to redirect echo's output to $APP_NAME"_Deploy.cfg". What's this string after expansion?
# 3  
Old 05-26-2016
Java

Quote:
Originally Posted by RudiC
Your code snippet doesn't have 7 lines, so I'd be inclined to look for the error somewhere else! Why don't you post the entire script?

In line 3 of your snippet, you seem to want to redirect echo's output to $APP_NAME"_Deploy.cfg". What's this string after expansion?
It is line 3 that is throwing the error instead of line 7 asthe preceding lines in my script are comments.

i am trying to append "_Deploy.cfg" to the filename after $APP_NAME variable.

So if $APP_NAME is "flipkart" my filename should be flipkart_Deploy.cfg

You can use my code snippet to replicate the error on Solaris.
# 4  
Old 05-26-2016
What's above string after variable's expansion?
# 5  
Old 05-26-2016
Quote:
Originally Posted by mohtashims
I have the below lines of code.
Code:
ls found*.tmp | while IFS= read -r entry; do
APP_NAME=$(sed -n -e 's/^.*<name>//' -e 's|</name>.*$||'p $entry | sed 's/\(<name>\|<\/name>\)//g')
echo "APP_NAME="$APP_NAME>$APP_NAME"_Deploy.cfg"
done

I m getting the below error while running the above script.
These lines are completely bogus anyways, there is little sense in debugging them.

You are doing a
Code:
ls found*.tmp | while IFS= read -r entry; do
   [...something...]
done

and my suspicion is that you want to read the contents of all files named "found*.tmp" mince that through your while-loop. What you do in fact is to expand "found*.tmp", which will give you a list of files, something like

Code:
found1.tmp found2.tmp found3.tmp foundfoo.tmp [...]

then you present this list to "ls", which will do nothing more than list it (again), resulting in this list:

Code:
found1.tmp
found2.tmp
found3.tmp
foundfoo.tmp
[...]

Notice that the only thing "ls" has done for you is to convert the separating spaces to linefeeds. Now, you take one after the other of these filenames and present them to sed, as input files, one after the other and every time you invoke sed in a separate subshell.

May i ask you a personal question: if you go to a chinese restaurant, do you call the waiter separately for each order of a single grain of rice or do you call him once and tell him to bring "the big rice plate, with everything on it"? Because this is like you treat poor old sed in your script: instead of presenting it a single stream to work on you call it once every time you got some characters to process.

Here is how you can do the same in perhaps a fraction of the time:

Code:
cat found*tmp | sed -n 's/.*<name>/;s/<\/name>.*/_Deploy.cfg/p'

This line will have the same limitations your original would have had, if you would have written it syntactically correct: it will only work correctly if there is only one "<name>...</name>" tag pair on every single line of input and none of these pairs span more than one line. Also, the script takes no caution for superfluous spaces. The following lines anywhere in your input will make the commands output probably not what it is intended to be and/or unusable (just like your script, would it have been correct):

Code:
<name>foo</name> foo <name>bar</name>
<name>foo
</name>
<name>  foo  </name>
<name>foo</ name>

I hope this helps.

bakunin

Last edited by bakunin; 05-26-2016 at 04:39 PM..
These 2 Users Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to solve ambigious redirect error?

hi all, i had the below script filename = /osa/data1/output.txt printf '%27s%53s\n' ' CURRENT DATE' 26-08-2014 >> $filename iam getting the ambigiuos redirect error in the 2nd line of the code...please guide me regards, vasa saikumar (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

2. Linux

Ambiguous redirect error and syntax error when using on multiple files

Hi, I need help on following linux bash script. When I linux commands for loop or while loop on individual file it runs great. but now I want the script to run on N number of files so it gives me ambiguous redirect error on line 12 and syntax error on line 22 : (pls help ); #!/bin/bash #... (16 Replies)
Discussion started by: Madhusudan Das
16 Replies

3. Shell Programming and Scripting

ambiguous redirect error

This script has ambiguous redirect error. ... cd $HOME cd folder/work # search all subfolders in work directory find -mindepth 1 -maxdepth 1 -type d | while read directory do CUR_FOLDER="${directory#"./"}" cd $CUR_FOLDER chmod 644 * for ff in *; do if ; then ... (5 Replies)
Discussion started by: candyme
5 Replies

4. Shell Programming and Scripting

Redirect Output and Error in 2 different files

hi, i want to redirect my output & error if generated to two different files. I have written the code for redirecting the output, i dnt have ne idea how to go abt it for errors. I tried spooling which was given in one of the threads on this forum.But it didn't work.The script i wrote as a lot... (4 Replies)
Discussion started by: bankimmehta
4 Replies

5. Shell Programming and Scripting

Ambiguous output redirect error

Hi everyone, While I was trying to do DATE=`date +"%Y%m%d_%H%M%S"` STARTLOG=$TUXSTDDIR/start_$DATE.log tmboot -y > $STARTLOG 2>&1 I got an error i.e. Ambiguous output redirect error. Here the first part is to boot the account so there is nothing wrong with that.... (6 Replies)
Discussion started by: pareshan
6 Replies

6. Shell Programming and Scripting

[BASH] redirect standard error and use it inside

Hi all, Maybe my question is too simple but till now i couldn't figure about a solution :( I have a bash script scheduled in cron: <cron time parameters> my_script.sh > result.log 2>&1 By this way i can have standard output and standard error in my result.log file Now i want my script... (2 Replies)
Discussion started by: Pescator
2 Replies

7. Shell Programming and Scripting

How redirect output(error and normal) to 2 different files

Hello, I have a java program which i am calling in shell script. I wanted to redirect output to 2 differetn files. Output should have both 1 & 2 (normal and error) in both file. pls help (2 Replies)
Discussion started by: balareddy
2 Replies

8. UNIX for Dummies Questions & Answers

redirect standard error into log file

Hi, I am new in shell scripting. Can anyone point out what wrong of below script. If I want the error output to "sqlerror.log" and database pool data output to "bulk_main.dat". Right now, the below script, if successful execute, the data will output to bulk_main.dat && sqlerror.log both... (7 Replies)
Discussion started by: epall
7 Replies

9. Shell Programming and Scripting

Ping: redirect output and error

Hi, I use this function (now modified with elif) for check if a pc is up: check_pc() { $PING $PC 1 2> /dev/null if ; then check_dir #Other function echo "Begin backup operation for $PC" echo "$SMBTAR -s $PC -u $USER -p $PASS -x $SHARE$EXCL -t - | gzip -c >... (3 Replies)
Discussion started by: mbarberis
3 Replies

10. Programming

How to redirect error message?

Hi, I was trying to cat some files but not all of them exist. I would get some messages like below: cat: cannot open /mil2_usr1/time_logs/investigator/6334 cat: cannot open /mil3_usr1/time_logs/investigator/6334 cat: cannot open /mil3_usr1/time_logs/investigator/6352 I don't want to see... (1 Reply)
Discussion started by: whatisthis
1 Replies
Login or Register to Ask a Question