Ambiguous redirect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ambiguous redirect
# 1  
Old 10-01-2010
Ambiguous redirect

Hello there,

I'm totally new in bash programming and ran into my first problem.

My script should generate 3 textfiles where the content of the first and the third row are the same in each file. Only the second row is different.

This is what I did in a very simplified explanation:
Code:
#!/bin/bash

text1="This goes into row 1 of each file"
text2="This goes into row 3 of each file"
text3="This one goes into file red.txt in row 2"
text4="This one goes into file yellow.txt in row 2"
text5="This one goes into file blue.txt in row 2"


file1=( "$text3" "red.txt" )
file2=( "$text4" "yellow.txt" )
file3=( "$text5" "blue.txt" )

for (( i=0;i<2;i++ )); do
  eval specialtext=\${file${i}[0]}
  eval filename=\${file${i}[1]}
  echo -e $text1"\n"$specialtext"\n"$text2 > $filename
done

This results in:
Code:
line 17: $filename: ambiguous redirect

What did I wrong?
Perhaps there is a much easier way to solve my problem?

Thank you for your comments!
# 2  
Old 10-01-2010
Something wrong with the counter of the loop?
Quote:
for (( i=1;i<4;i++ )); do
# 3  
Old 10-01-2010
No, there seems nothing to be wrong with the counter.
I use to count until <2 because it switches the array elements of the file1, file2 and file3 vars.
I use this array to pair the special text with the filename that belongs to the special text.

Maybe there's a completely different way to solve this?
Or I'm just not seeing the wood for the trees!

JohnDoe
# 4  
Old 10-01-2010
I think Franklin is right. File0 never gets set for instance. The i counter varies file not the elements; for this you use 2 fixed values 0 and 1.
# 5  
Old 10-01-2010
Thanks to Franklin52 and Scrutinizer! Smilie

A simple change from
Code:
for (( i=1;i<4;i++ )); do

to
Code:
for (( i=0;i<2;i++ )); do

solved this.

Where have my eyes been?
Thank you!

---------- Post updated at 09:24 PM ---------- Previous update was at 09:13 PM ----------

Now I have another question:
Is there a possibility to make a multidimensional array?

Something like this:
Code:
file[0]=( "$text3" "red.txt" )
file[1]=( "$text4" "yellow.txt" )
file[2]=( "$text5" "blue.txt" )

So I could count the elements of the file array and use this instead of <3.

Last edited by johndoe; 10-01-2010 at 04:30 PM..
# 6  
Old 10-01-2010
AFAIK that is not possible. You'll have to emulate, for example by using eval or by using ksh93 and associative arrays with compound indexes.

---------- Post updated at 21:50 ---------- Previous update was at 21:37 ----------

But why don't you use something like this:
Code:
#!/bin/bash

text1="This goes into row 1 of each file"
text2="This goes into row 3 of each file"
text3="This one goes into file red.txt in row 2"
text4="This one goes into file yellow.txt in row 2"
text5="This one goes into file blue.txt in row 2"

ftext=( "$text3" "$text4" "$text5" )
file=( "red.txt" "yellow.txt" "blue.txt" )

for (( i=0;i<3;i++ )); do
  echo -e "$text1\n${ftext[i]}\n$text2" > "${file[i]}"
done

# 7  
Old 10-01-2010
Hey, this is a quite simple solution. Smilie
I'm a little too cumbersome.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

$1”: ambiguous redirect

New to the site, please let me know I'm not meeting the post guidelines. I'm creating a bash script to generate a report with output from a grep command. The goal is to direct the output to a different log file by using a 'logger file'. But I get this error during the run: $1: ambiguous... (5 Replies)
Discussion started by: dallas88
5 Replies

2. Shell Programming and Scripting

Ambiguous output redirect in xterm

Hi all, I've been working on a bash script to help with backups that I have to do at work. One of the lines in the script is supposed to launch an xterm, log into a specific server node and launch a tar backup to tape. This part works ok, but I've been trying to get stdout and stderr to... (2 Replies)
Discussion started by: Exitalterego
2 Replies

3. 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

4. Shell Programming and Scripting

Receiving 'ambiguous redirect' when trying to run command against multiple files

I came across the command string on https://www.unix.com/shell-programming-scripting/141885-awk-removing-data-before-after-pattern.html which was what I was looking for to be able to remove data before a certain pattern. However, outputting the result to a file seems to work on an individual basis... (4 Replies)
Discussion started by: HLee1981
4 Replies

5. 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

6. UNIX for Dummies Questions & Answers

ambiguous redirect issue

I am trying to run the following script and I am getting an "ambiguous redirect" error. I have checked to make sure that the files are all where I have specified and are read/write as needed. Any ideas? Note: I have removed the actual path info for privacy sake. I have triple checked to make... (1 Reply)
Discussion started by: malantha
1 Replies

7. Shell Programming and Scripting

> to empty files, but ambiguous redirect

Hi Everyone, # ll total 0 -rw-r--r-- 1 root root 0 2010-05-13 11:29 a1.log -rw-r--r-- 1 root root 0 2010-05-13 11:29 a2.log -rw-r--r-- 1 root root 0 2010-05-13 11:29 a3.log # rm a.log above rm no problem, but when i use "> a.log", it says "-bash: a.log: ambiguous redirect". ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. 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

9. Shell Programming and Scripting

`ls -l`: Ambiguous

Hi, I'm trying to code a simple script (c-shell) on a Solaris box and I'm getting an "Ambiguous" error. These are the lines that cause the error: On c-shell: > set var = "" > @ var = `ls -l` `ls -l`: Ambiguous However if I change the second line to: > set var = `ls -l` This works... (2 Replies)
Discussion started by: Guillermo Lopez
2 Replies

10. UNIX for Dummies Questions & Answers

ambiguous redirect

i have following statement in the script echo -e "$str_XML_col_name:$str_field_type;" >> $i_DC_Key_$i_Tgt_DC_key_Schema here $i_DC_Key is DC key and $i_Tgt_DC_key are the variables............... when i ran the script i am getting error rec_merge.sh: $i_DC_Key_$i_Tgt_DC_key_Schema:... (1 Reply)
Discussion started by: mahabunta
1 Replies
Login or Register to Ask a Question