Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm making a script for monitoring some user processes with Upstart but, since its for the company I work for, they asked me to do it generic...how's so? Well, the number of processes being monitored could vary, as in number as in name so I need to create it so a user can input "n" number of processes and give the shell its pathname's so I can store them in variables and pass Upstart a script with them so my question is: how can I create as many variables as needed?

Here's my script so far…

./script input

#!/bin/bash

FILENAME=$1
i=1

while read LINE
do
[rutaServ$i]=$LINE
echo "ruta -> [rutaServ$i]"
i=`expr $i + 1`
done < $FILENAME

As you can see I am taking the pathname provisionally from a plain text file with nothing more than /home/user lines in it but, when I run this script I get the following error [rutaServ1]=/home/user: The file or directory doesn't exists

What is the correct way of doing this?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

It looks like your array syntax is off just a bit. Also, there's no need for the index variable; you can use the += operator to append to an array.

#!/bin/bash

FILENAME=$1

rutaServ=()
while read LINE
do
rutaserv+=($LINE)
echo "ruta -> $LINE"
done < "$FILENAME"

bash v4 has a new command, mapfile (or readarray) to read the contents of a file into an array all at once:

mapfile rutaServ < "$FILENAME"

To access values from the array:

for x in "${rutaserv[@]}"; do
    echo "$x"
done
share|improve this answer
    
Ok, tried this and worked but after the loop if I try something like echo $LINE I get nothing in the output. How can I access the values of the array? –  Kronos May 13 '13 at 16:58
    
great! that worked! Now I have a doubt, what does the "@" stands for in the index array? –  Kronos May 13 '13 at 17:24
    
Think of it as "a for all": it expands the array to a list of words, one word per element in the array. It is quoted so that any element that contains whitespace will be treated as a single word. –  chepner May 13 '13 at 17:38
add comment

Could the script be called like script.sh process1 process2? Then you could refer to the arguments with $@:

# set process1 process2 # set arguments for testing

for x in "$@"; do 
    echo "ruta -> $x"
done

[rutaServ$i]=$LINE should be rutaServ[$i]=$LINE or rutaServ+=("$LINE").

share|improve this answer
    
I tried your suggestion for the index before I try the arguments option because, I don't know how many processes will be, it could take long to write all arguments. So...here's my new code where in the end I test the variables... while read LINE do rutaServ[$i]=$LINE echo "ruta -> $rutaServ$i" done < "$FILENAME" i=1 while [ $i -le 3 ] do echo $rutaServ$i i=expr $i + 1` done` Now it works as saving the variables but when printing, it appends a number to the path: /home/user/ruta31 /home/user/ruta32 /home/user/ruta33 –  Kronos May 13 '13 at 17:12
    
You can access array elements like echo "${rutaServ[$i]}". –  Lri May 13 '13 at 17:23
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.