LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash arrays and permissions denied? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-arrays-and-permissions-denied-733977/)

shoemoodoshaloo 06-18-2009 02:49 PM

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.

Uncle_Theodore 06-18-2009 03:03 PM

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?

shoemoodoshaloo 06-18-2009 03:07 PM

Quote:

Originally Posted by Uncle_Theodore (Post 3578734)
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.

chrism01 06-18-2009 06:05 PM

Note that in a variable assignment, there must be no spaces on either side of the '='.

David the H. 06-18-2009 06:29 PM

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.

chrism01 06-18-2009 06:34 PM

Actually, if you just want an empty file, use

touch filename

much easier to read/debug than '> filename'


All times are GMT -5. The time now is 01:09 PM.