Visit Our UNIX and Linux User Community


Shell variables problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell variables problems
# 1  
Old 09-25-2009
Shell variables problems

hi, i need some help, the situation is this

1-file of variable enviroments
DIR1=/tmp
DIR2=otherdir/mydir

2-file of list of files (all the names references whic variables of first point)

${DIR1}/${DIR2}/onefile

Well now i create a shell script whic this content
Code:
              . ./file_of_env
              for file in `cat list_of_files`
              do
                 if [ -f ${file} ]
                 then
                      .....
                  else
                     ......
                  file
              done

Well this not work because the valor of variable "file" is :

${DIR1}/${DIR2}/onefile

but if inside the while i put and echo "$DIR1" then enviorement variable is charge.
What i'm doing wrong?
who can i make for the system make the sustituion of the enviorement variable DIR1 and DIR2?

other probe i make is chage the
. ./file_of_env

and put directly the variable definitios of DIR1 and DIR2 but tthe result is the same.

thanks.

---------- Post updated at 05:47 AM ---------- Previous update was at 04:32 AM ----------

i resolve the problem.
thread can be closed.
# 2  
Old 09-25-2009
Please, show us how you have resolved the problem.
Something like that ?
Code:
              . ./file_of_env
              for wfile in `cat list_of_files`
              do
                 eval file=$wfile
                 if [ -f ${file} ]
                 then
                      .....
                  else
                     ......
                  file
              done

Jean-Pierre.
# 3  
Old 09-25-2009
thanks for answer you option is so better than my.
my solution was to make a subshell inside my shell principal and execute:
something like this:
Code:
cat file_env >new_shell.sh
for fich_include in `cat list_file`
do
   buffer="$buffer $fich_include"
done

 echo "BUFFER=\"$buffer\"" >> new_shell.sh
echo "for fichero in \$BUFFER " >>new_shell.sh
 echo "do " >>new_shell
echo "if [ -f \$fichero  ]" >>new_shell
 echo "then">>new_shell
echo " echo [OK ">>new_shell
.....
sh ./new_shell

but repeat your solution is more clean and better. now i modify my script whith your option.
thanks some much.
# 4  
Old 09-25-2009
Quote:
Originally Posted by aigles
[code]
for wfile in `cat list_of_files`

That is a UUOC and will break the script if any filenames contain whitespace.
# 5  
Old 09-26-2009
Thank you cfajohnson, the list of files does not contain blanks.
Normally I use :
Code:
while read line
do
....
done < file_list

i doesn't know UUOC's meaning, I it have looked in google and found other UUOC's cases that i doesn't know they are "bad code" or not optimus code, thank you very much for your contribution.
# 6  
Old 09-26-2009
Quote:
Originally Posted by chipcmc
Thank you cfajohnson, the list of files does not contain blanks.
Normally I use :
Code:
while read line
do
....
done < file_list

i doesn't know UUOC's meaning, I it have looked in google and found other UUOC's cases that i doesn't know they are "bad code" or not optimus code.

At the least, an unnecessary cat is a waste of resources (cat is an external command, and therefore much slower than using shell syntax).

At the worst, it will fail. If you use later that script on another file that does have spaces in file names, it will fail. The redirection syntax will always work, giving you a more robust script.

Previous Thread | Next Thread
Test Your Knowledge in Computers #271
Difficulty: Easy
While working at Bletchley Park, in the first week of June each year Alan Turing would get a bad attack of hay fever, and he would cycle to the office wearing a service gas mask to keep from breathing pollen. In the first week of June each year he would get a bad attack of hay fever, and he would cycle to the office wearing a service gas mask to keep from breathing the pollen.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

2. Shell Programming and Scripting

Problems with substitution between two variables

To all geeks, What I want to achieve: 1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2) 2. Check if files exisits. If doesn't, then exit 3. If files exist, look for a particular string in both files 4. If the string exists, then change the... (8 Replies)
Discussion started by: Deepak Tulsani
8 Replies

3. Shell Programming and Scripting

Problems with variables syntax

Hi there I am really struggling :eek: to place a value in a variable with the following loop, having run out of ideas please can someone point me in the right direction? We first read two PIDs of a program (say calc) into an array, then we loop reading the details of those processes into a... (6 Replies)
Discussion started by: nathan.harris
6 Replies

4. Shell Programming and Scripting

Two problems when I am writing my own shell

Hi, guys: I am writing my own shell using c. I meet with two problems. First, when I use tcsetpgrp system call to move a background job to foreground, when this job finishes, my shell program also exit. What is the problem for that? Second, when I use signal , if the child has its own... (1 Reply)
Discussion started by: tomlee
1 Replies

5. Shell Programming and Scripting

Problems with expect and set variables

I'm writing a script that'll send a time-stamp to my backup server. I create a file with the name of the current date, send it to my server with scp and rm the file from the local computer. Individually these commands work fine and with a set name the expect scripts also work fine. The problem... (0 Replies)
Discussion started by: Ktesh564
0 Replies

6. UNIX for Dummies Questions & Answers

Problems in shell

hiiii this is the first time for me here >>> but really i have some problems for answers these things also understanding... Can any one help me? Write a simple shell as given in class. Your shell, called shell will read input lines from standard input, parse them into a... (3 Replies)
Discussion started by: Ronald kabbi
3 Replies

7. Shell Programming and Scripting

problems calling out variables in a loop

good afternoon forums. i have a problem that ive been trying to work out all morning and cant seem to get my head around it. what i have in my script is individual letters saved in different variables. so if the word unix was saved then 'u' would be stored in the variable 'wvar1' 'n' in 'wvar2'... (7 Replies)
Discussion started by: strasner
7 Replies

8. Shell Programming and Scripting

Shell script problems to do

Does anyone know a good site to do shell script problems? (0 Replies)
Discussion started by: cleansing_flame
0 Replies

9. UNIX for Dummies Questions & Answers

Problems with sed and flat file variables

Hello All, It has been a loooooooooooong time since I had last used sed but decided to use it for a simple task I have . My goal is to use sed to read variables from a flat file then use those same variables in order to make some subsitutions. However what I am finding is that when the... (1 Reply)
Discussion started by: icalderus
1 Replies

10. Shell Programming and Scripting

C Shell problems

I am a newbies here and also dummies in csh language. I've been told from my frenz this forum is very useful site for Q&A about UNIX script. Can anyone in here just lead me to learn more about csh language with code example. tq (3 Replies)
Discussion started by: unknown2205
3 Replies

Featured Tech Videos