LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-18-2009, 02:49 PM   #1
shoemoodoshaloo
LQ Newbie
 
Registered: Jun 2009
Posts: 18

Rep: Reputation: 0
Bash arrays and permissions denied?


I have an array of names:

Code:
Name_List=(TAR1 MIR L2)
What I want to do is this:

For each name in the array, open a blank file called name.txt
So for example, in the end, I should have
TAR1.txt
MIR.txt
L2.txt

Each being an empty file. Here is my attempt:

Code:
#!/usr/bin/bash

#Script to take in a list of names, and parse a large data file using new_processdata.py module


ROOTDIR=.


Name_List=(TAR1 MIR L2)

element_count=${#Name_List[@]}
index=0


mkdir $ROOTDIR/Test

while [ "$index" -lt "$element_count" ]
do 
	temp_file= echo ${Name_List[$index]}
	> $temp_file	
	

	#Make a file for each name
	
	let "index = $index + 1"
done
With errors: TAR1
processdata.sh: line 20: $temp_file: ambiguous redirect
MIR
processdata.sh: line 20: $temp_file: ambiguous redirect
L2
processdata.sh: line 20: $temp_file: ambiguous redirect


Can anybody help me out? It would be quite appreciated.
 
Old 06-18-2009, 03:03 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
I think you're looking for something like this:
Code:
teddy@office~/$ Name_List=( TAR1 MIR L2 )
teddy@office~/$ i=0; while [ ${Name_List[$i]} ]; do echo ${Name_List[$i]}>${Name_List[$i]}.txt; i=$((i+1)); done
teddy@office~/$ ls
L2.txt  MIR.txt  TAR1.txt
The problem in your script is in this line:

temp_file= echo ${Name_List[$index]}
> $temp_file

What exactly do you assign to the variable temp_file?

Last edited by Uncle_Theodore; 06-18-2009 at 06:09 PM.
 
Old 06-18-2009, 03:07 PM   #3
shoemoodoshaloo
LQ Newbie
 
Registered: Jun 2009
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Uncle_Theodore View Post
I think you're looking for something like this:
Code:
teddy@office~/$ Name_List=( TAR1 MIR L2 )
teddy@office~/S i=0; while [ ${Name_List[$i]} ]; do echo ${Name_List[$i]}>${Name_List[$i]}.txt; i=$((i+1)); done
teddy@office~/$ ls
L2.txt  MIR.txt  TAR1.txt
The problem in your script is in this line:

temp_file= echo ${Name_List[$index]}
> $temp_file

What exactly do you assign to the variable temp_file?
Well, I thought I had assigned to the variable temp_file, the output of "echo ${Name_List[$index]}" which would be the element in the array.

Than kyou for posting the working code, I will try to fix mine now.
 
Old 06-18-2009, 06:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Note that in a variable assignment, there must be no spaces on either side of the '='.
 
Old 06-18-2009, 06:29 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Also, in order to use the output of one command sequence inside another command (such as variable setting), you need to enclose it in the $() command substitution structure. And wrap the whole thing in quotes for good measure.
Code:
temp_file="$(echo ${Name_List[$index]})"	

(But echo isn't really be necessary in this case)

temp_file="${Name_List[$index]}"

should set temp_file to the contents of the index number.
Also, the '>' is a file redirect. It dumps the output of the previous command into a file, creating it if it doesn't exist. In your case "> $temp_file" was an "ambiguous redirect" because there was no previous output to send into the file (The previous command was setting a variable, which gives no output).

Unless you really intended to do something like this:
Code:
temp_file="filename.txt"
echo ${Name_List[$index]} > $temp_file
Which creates a text file called "filename.txt", and puts the value of the array entry inside it.
 
Old 06-18-2009, 06:34 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Actually, if you just want an empty file, use

touch filename

much easier to read/debug than '> filename'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing arrays in bash Raht Programming 1 03-12-2009 12:33 PM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 10:49 PM
bash arrays question introuble Programming 1 05-20-2006 03:07 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration