Problems with substitution between two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with substitution between two variables
# 1  
Old 09-15-2012
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 filenames (FILE1 and FILE2) to the string that was looked up in the #3
5. If string does not exist, then do nothing
6. Build up another filename (FILE3) which is some combination of FILE1 and FILE2

Here's the code that I have written. Everything else works fine EXCEPT for point # 4. The variable substitution just doesn't work. I've tried many modifications, and this is the last one which still doesn't work.
Code:
#!/bin/bash

echo "Enter first filename:"
read FILE1
echo "Enter second filename:"
read FILE2

HOSTN="`grep "system hostname" $FILE1 | awk '{print $3}'`"
echo $HOSTN

if [[ -z "$HOSTN" ]]
  then
    :
  else
    FILE1=$HOSTN
    echo "$FILE1 , $HOSTN "
fi
HOST="`grep "system hostname" $FILE2 | awk '{print $3}'`"
echo $HOST
if [ -n "$HOST" ]
  then
   FILE2=$(grep "system hostname" $FILE2 | awk '{print $3}')
   echo "$FILE2 , $HOST "
fi

echo "Output follows..."


FILE3="${FILE1%%.*}""_""${FILE2%%.*}"
echo "$FILE1, $FILE2, "${FILE1%%.*}", "${FILE2%%.*}", $FILE3"

The outputs:

1. When the string exists in the file:
Code:
deepak@ubuntu:~/Desktop/VF-NL$ ./test.sh 
Enter first filename:
sample.cfg
Enter second filename:
sample1.cfg
IAGG03LDV
 , IAGG03LDV
IAGG03FTC
 , IAGG03FTC
Output follows...
_IAGG03FTCV

2. When the string does not exist in the file:
deepak@ubuntu:~/Desktop/VF-NL$ ./test.sh 
Enter first filename:
RM_CGA_apns.txt
Enter second filename:
UT_CGA_apns.txt


Output follows...
RM_CGA_apns.txt, UT_CGA_apns.txt, RM_CGA_apns, UT_CGA_apns, RM_CGA_apns_UT_CGA_apns
deepak@ubuntu:~/Desktop/VF-NL$

Like I said, I have tried many combinations, but nothing seems to be working right. THe variable substitution works well when I do it outside the if loop. but within the if loop, it just doesn't work.

OS:
deepak@ubuntu:~/Desktop/VF-NL$ uname -a
Linux ubuntu 2.6.38-11-generic #50-Ubuntu SMP Mon Sep 12 21:18:14 UTC 2011 i686 i686 i386 GNU/Linux
deepak@ubuntu:~/Desktop/VF-NL$

---------- Post updated at 08:41 PM ---------- Previous update was at 08:37 PM ----------

Been breaking my head since yesterday on what is going wrong with a simple substitution, but unfortunately can't seem to get it. Help from you geeks will be greatly appreciated.

Regards,
Deepak


Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 09-15-2012 at 12:24 PM..
# 2  
Old 09-15-2012
1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2)

#You can follow the same with your methods.

2. Check if files exisits. If doesn't, then exit'
Code:
if [[ -e $FILE1 && -e $FILE2 ]] # Check files present or not.
then
#do below operation
else
#exit
fi

3. If files exist, look for a particular string in both files

#To check string present or not..
Code:
if [[ `grep "system hostname" -c $FILE1` -gt 0 ]]
then 
HOSTN=$(awk '/system hostname/{print $3}' $FILE1)
else
#exit 
fi

4. If the string exists, then change the filenames (FILE1 and FILE2) to the string that was looked up in the $3
Code:
mv $FILE1 $HOSTN  #Rename files

5. If string does not exist, then do nothing

#In above if part do exit

6. Build up another filename (FILE3) which is some combination of FILE1 and FILE2
# 3  
Old 09-15-2012
Are you sure there's no <carriage return> char (^M, 0x0D) in your filenames? The output I see makes perfect sense if assuming a <cr> at the end of the names.
# 4  
Old 09-15-2012
Like $RudiC says, there are probably some characters causing the "error", perhaps a space as the first character in HOSTN?
Change this line in your script to find out if the problem is that:
Code:
     FILE1="$HOSTN"

(using double quotes)
# 5  
Old 09-16-2012
Thanx a lot for your response guys.

@pamu: I generally don't use short methods of writing code because of nature of my work. Using elaborate code makes it easier to understand for those who replace me. I did try those what you suggested, but the problem seems to be with the variable substitution than anything else.

@RudiC, @244an: See below:

HOSTN="`grep "system hostname" $FILE1 | awk '{print $3}'`"
VAR="$HOSTN"
echo "Values outside the loop are: $HOSTN, $VAR"

if [[ -z "$HOSTN" ]]
then
:
else
FILE1="$HOSTN"
echo "$FILE1 , $HOSTN "
fi
HOST="`grep "system hostname" $FILE2 | awk '{print $3}'`"
echo $HOST
if [ -n "$HOST" ]
then
FILE2=$(grep "system hostname" $FILE2 | awk '{print $3}')
echo "$FILE2 , $HOST "
fi

deepak@ubuntu:~/Desktop/VF-NL$ grep ^M sample.cfg | more
deepak@ubuntu:~/Desktop/VF-NL$
deepak@ubuntu:~/Desktop/VF-NL$
deepak@ubuntu:~/Desktop/VF-NL$
deepak@ubuntu:~/Desktop/VF-NL$ grep ^M sample1.cfg | more
deepak@ubuntu:~/Desktop/VF-NL$
deepak@ubuntu:~/Desktop/VF-NL$

I can't find any DOS characters in the file. Infact when I saved these files, I saved them in UNIX format intentionally because I know what mess the DOS characters an make. Also the "HOSTN" doesn't work either. I had tried it several times in my earlier attempts, but that didn't work (like now). But I think you guys are right. It has to do with something in the line that is filtered out. Also the fact that the string that I am trying to filter out is at the end of the line. Since the character is at the end of line, something else is getting tagged along with the string. I'll try with some other files and see if that works. If there are any more ideas, please share.

---------- Post updated at 12:34 PM ---------- Previous update was at 12:31 PM ----------

Quick Follow up:

I created two dummy files with strings that appear in my original files, and it seems to work!

deepak@ubuntu:~/Desktop/VF-NL$ ./test.sh
Enter first filename:
a.txt
Enter second filename:
b.txt
Values outside the loop are: XYZ, XYZ
XYZ , XYZ
ABC
ABC , ABC
Output follows...
XYZ, ABC, XYZ, ABC, XYZ_ABC

I'll see what's wrong with the original file and update. Thanx again guys!

Regards,
Deepak
# 6  
Old 09-16-2012
Can someone please explain what the $(....) is doing in the below 2 lines of code? Is $(....) equivalent to `....` to allow the commands to run?

Code:
FILE2=$(grep "system hostname" $FILE2 | awk '{print $3}')
HOSTN=$(awk '/system hostname/{print $3}' $FILE1)

# 7  
Old 09-16-2012
@Deepak Tulsani: Pls post the output of sth like od -An -tx1 yourfile to see if grep greps control chars.

@mjf: Yes, at least in the sh family, $(...) is the new form of command substitution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

2. 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

3. UNIX for Dummies Questions & Answers

sed substitution and shell variables

Hello!! Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing. for ex. i've used the follow sed command.. sed "s/$var1/$var2/g" i've also tried the other... (5 Replies)
Discussion started by: a_ba
5 Replies

4. Shell Programming and Scripting

[bash] command line substitution with environmental variables

Hi, I'm using an array that contains compiler FLAGS that need to be executed either before ./configure or after the main 'make' command. example of array containing compiler flags. ------------------------------------------------- FLAGS="CFLAGS=\"-arch x86_64 -g -Os -pipe... (7 Replies)
Discussion started by: ASGR
7 Replies

5. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

6. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: chipcmc
5 Replies

7. UNIX for Advanced & Expert Users

shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION) value in a kernel Makefile. So, I have a shell script that takes one parameter, a version string: buildkernel.sh 2.6.18.21.7-custom In the script, I assign the parameter to K_VER: K_VER="2.6.18.21.7-custom"... (2 Replies)
Discussion started by: duderonomy
2 Replies

8. Shell Programming and Scripting

Double Substitution variables in ksh

Hi I have a variable whose value is like this i=/test/test1/test2/myfile.cd.070505123457 i would like to have the value of myfile.cd stored into another variable my attempt is test=${i##*/} ;echo $test ##and i get myfile.cd.070505123457 since what i wnat is myfile.cd i try this... (19 Replies)
Discussion started by: xiamin
19 Replies

9. Shell Programming and Scripting

command substitution problems with csh using grep

I am trying to set a command into a variable in a csh script using command substituion with ``. I am having a problem with ps command combined with grep. The command is as follows (shows processes running with the word gpts in them). /usr/ucb/ps axwww | grep gpts this works fine at the... (2 Replies)
Discussion started by: voodoo31
2 Replies

10. Shell Programming and Scripting

sed substitution problems

Hi falks, I need to substitute in my ksh program, parameter (full path of directory) ,which is sent from outside program, with another parameter (another full path of directory) ,which is known and validated inside my program. I tried to use "sed" ,but i failed. For example: ... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question