Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How can I read file in shell script , then assign each line to an variable that i can use later ,,,(am thinking in way to load an default setting from file)

i already try :

process (){

}

FILE=''
read -p "Please enter name of default file : " FILE

if [ ! -f $FILE  ]; then

    echo "$FILE : does not exists "
    exit 1
elif [ ! -r $FILE  ]; then

    echo "$FILE : can not read "
fi

exec 0<"$FILE"
n=0
while read -r line
do
   (assign each line to an variable) 
done
share|improve this question
    
any way (concept) that to load an default parameter that user saved in (file) , would be accepted :) –  moata_u Mar 10 '11 at 7:09
    
Using . /path/to/config is the best approach for setting defaults, but if you need to set lines of a file to an array variable (as your question title suggests), bash 4.0 has new builtin commands called readarray and mapfile. –  Mikel Mar 10 '11 at 19:48
add comment

2 Answers

up vote 10 down vote accepted

For configuration purposes it's probably easiest to define the parameters in the configuration file in bash syntax and later source it using . /path/to/config.

Example default.cfg:

parameter_a=100
parameter_b=200
parameter_c="Hello world"

Example script.sh:

#!/bin/bash

# source the default configuration
. /path/to/default.cfg

echo $parameter_a
echo $parameter_b
echo "$parameter_c"

...

If you don't like that approach you can also read the lines into an array:

while read line
do
    array+=("$line")
done < some_file

To access the items you would then use ${array[index]}, e.g.:

for ((i=0; i < ${#array[*]}; i++))
do
    echo "${array[i]}"
done

(Where ${#array[*]} is the size of the array.)

Read more about arrays in bash here.

share|improve this answer
    
Thanks that was very helpful..100% –  moata_u Mar 10 '11 at 8:53
    
I'd use array+=("$line") rather than array[${#array[*]}]="$line", or if one needs to support bash older than v3.1, array[i++]=$line (where you make sure to set i=0 before the loop of course). –  geirha Mar 10 '11 at 16:13
    
I too recommend the source-approach btw. Though I'd use lowercase variable names rather than all uppercase... to avoid overwriting environment variables or internal shell variables. –  geirha Mar 10 '11 at 16:22
    
@geirha: Thanks (once again) for your comments! Didn't know about the +=() syntax, which is definitely easier to read (and write). –  htorque Mar 10 '11 at 17:23
add comment
c=0 # counter
# read whole file in loop
while read line
do
  textArray[$c]=$line # store line
  c=$(expr $c + 1) # increase counter by 1
done < $FILE
# get length of array
len=$(expr $c - 1 )

# use for loop to reverse the array
for (( i=$len; i>=0; i-- ));
do
  echo ${textArray[$i]}
done
share|improve this answer
1  
Don't use expr in bash, bash can do everything expr can, and better. The first "if" there makes no sense, you are not checking if a file exists... Lastly, quote all parameter expansions; "$foo", not $foo. See mywiki.wooledge.org/Arguments –  geirha Mar 10 '11 at 16:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.