Solved

In Unix bash shell script, how to extract a variable from a file and use that varibale to include in another filename?

Posted on 2012-04-09
20
696 Views
Last Modified: 2012-04-10
I am trying to open file "file1.dat" and extract values from variable 'var2', then input/include those values within two files' names as shown in the script below. I am currently getting the following error on the very last line of my script:
syntax error: unexpected end of file

mydir=/user/name
dir3=/year/month/week/day
export mydir
export dir3

cat $mydir/file1.dat | while read rcd      # reading file1.dat
 do
     echo $mydir/file1.dat  
 var2=$(echo ${rcd} | cut -b1-2)              # extracting values for var2 varible
  if -f ${dir3}/filename1.s${var2}   
   then 
    rm ${dir3}/filename1.s${var2}
           
   elif -f ${dir3}/filename2.s${var2}  
    then 
     rm ${dir3}/filename2.s${var2}

  else 
     echo nothing matched
  fi
done
exit 0

Open in new window


Any help with my script would be greatly appreciated!
0
Comment
Question by:labradorchik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +3
20 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37824751
" if -f " is wrong syntax. Use square brackets or use "if test -f ..."

if [ -f ${dir3}/filename1.s${var2} ] ; then
...

(Note the spaces around tthe brackets!)


if test -f ${dir3}/filename1.s${var2} ; then
...
0
 

Author Comment

by:labradorchik
ID: 37824853
Thank you!
I just made changes and run this script again, but still getting the same error:
syntax error: unexpected end of file

mydir=/user/name
dir3=/year/month/week/day
export mydir
export dir3

cat $mydir/file1.dat | while read rcd      # reading file1.dat
 do
     echo $mydir/file1.dat  
 var2=$(echo ${rcd} | cut -b1-2)              # extracting values for var2 variable
  if test -f ${dir3}/filename1.s${var2};   
   then 
    rm ${dir3}/filename1.s${var2}
           
   elif test -f ${dir3}/filename2.s${var2};  
    then 
     rm ${dir3}/filename2.s${var2}

  else 
     echo nothing matched
  fi
done
exit 0

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37824920
You should omit the comment in line 6 ("# reading file1.dat"). Some shells dont like hash characters ("#") after "read".
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37825004
... besides that I can't see anything unusual.

Did you post the whole script?
0
 

Author Comment

by:labradorchik
ID: 37825178
Thank you for your suggestions, but even when I omitted those all comments I still get the same error.
Yes, that was my whole script.

Note:  file1.dat file does exist in the /user/name directory.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 100 total points
ID: 37825431
Could it be that you use Windows to create/edit the script to then transfer it to Unix?
If so, could it further be that you're transferring via FTP in text mode instead of binary mode?

Text mode transfer will leave carriage-return characters at the line ends, which can make scripts fail.

Try "cat -v scriptname". Do you see "^M" at the line ends?

If so, remove these characters with

cp -p scriptname scriptname.tmp && tr -d "\r" < scriptname > scriptname.tmp && mv scriptname.tmp scriptname

(The initial "cp" command is meant to preserve ownership and permissions).
0
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 37825489
You have a lot of unnecessary code.

You can simplify the whole script to:

#!/bin/bash
mydir=/user/name
dir3=/year/month/week/day

while read rcd
do
  var2=${rcd:0:2}
  rm -f $dir3/filename[1-2].s$var2 || echo nothing matched
done < $mydir/file.dat
 

Open in new window

0
 

Author Comment

by:labradorchik
ID: 37825517
It is all my fault, I misspelled my directory name... Script sends emails now, but my atachment does not open. I can open my email and can I see the attached .txt file, but when I try to open the attachment it appears to be empty.

Note: there is no script in the .txt file, just one sentence for testing purposes. Also, I am using Unix bash script on Windows and Lotus iNotes for my email. I always type my script in the Unix bash shell, - not in the windows, so there is no transfering involved.

Do you know if this command may for some other Unix shell and not for bash shell?
0
 

Author Comment

by:labradorchik
ID: 37825545
Tintin, thank you for your comments, but my real code has many file names like file1.s$var2, file2.s$var2, file35.s$var2, filelast.s$var2, filenext.s$var2..., so I don't think I can use your example in my case.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37825566
I fear I don't fully understand your previous comment #37825517 ...
0
 

Author Comment

by:labradorchik
ID: 37825609
Sorry. The main issue now is that attached file does not open in the email. Attachment is empty, but I can see that it is attached in the email.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37825625
I don't see any email command in the script you posted.

Are you talking about our other case ("Body text in email")?

Sorry, I have to leave now. Let's continue in a few hours, please!

wmp
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37825638
Yes, that was my whole script.

So that is incorrect?   If so, could you please post the whole script.
0
 

Author Comment

by:labradorchik
ID: 37825656
Ok, sorry, I posted comment #37825517 in the wrong question!!
0
 

Author Comment

by:labradorchik
ID: 37825966
Please don't worry about my comment ID: 37825517, - I posted it in the wrong questions...

Below script is completed, - it is just a replica of the real one, which is much longer and has different files names. I am still getting this error:
syntax error: unexpected end of file

Do you see anything wrong in this script?

mydir=/user/name
dir3=/year/month/week/day
export mydir
export dir3

cat $mydir/file1.dat | while read rcd      
 do
     echo $mydir/file1.dat  
 var2=$(echo ${rcd} | cut -b1-2)              
  if test -f ${dir3}/filename1.s${var2};   
   then 
    rm ${dir3}/filename1.s${var2}
           
   elif test -f ${dir3}/filename2.s${var2};  
    then 
     rm ${dir3}/filename2.s${var2}

  else 
     echo nothing matched
  fi
done
exit 0

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 37826084
how are you running it?
Do you see anything if you run it verbose?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37826108
What output do you get when you do

bash -x scriptname
0
 
LVL 79

Expert Comment

by:arnold
ID: 37826222
You should enclose the filenames in "" this way if there are spaces in the name, it would not break the test.
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 150 total points
ID: 37826939
Couple or three comments:
- you don't need the semi-colon at the end of the "if" and "elif" lines - the new-line ends that statement.  You only need a semi-colon there if you want the "then" to be on the same line as the "if/elif".
- In your error line, you should say what you were looking for, e.g. echo nothing matched ${var2} in ${dir3}
- Could you use a modified version of Tintin's script, with a wildcard for the first part of the name?  e.g.
    rm $dir3/*.s$var2 2>/dev/null || echo nothing matched $var2 in $dir3
  (you can't use "rm -f" here, because it returns success whether or not it deleted a file)

Otherwise, the script looks fine - I've cut and paste your script, modified the mydir and dir3 values, and it runs without an error.  I've tried it in bash, ksh and dash, and all are happy.
0
 

Author Comment

by:labradorchik
ID: 37829715
simon3270, I did exactly as you mentioned in your comment and it works. I basicaly took  Tintin's script and made those changes and it works fine!! :) No more errors...
Thank you very much!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question