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

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!
labradorchikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
" 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
...
labradorchikAuthor Commented:
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

woolmilkporcCommented:
You should omit the comment in line 6 ("# reading file1.dat"). Some shells dont like hash characters ("#") after "read".
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

woolmilkporcCommented:
... besides that I can't see anything unusual.

Did you post the whole script?
labradorchikAuthor Commented:
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.
woolmilkporcCommented:
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).
TintinCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
labradorchikAuthor Commented:
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?
labradorchikAuthor Commented:
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.
woolmilkporcCommented:
I fear I don't fully understand your previous comment #37825517 ...
labradorchikAuthor Commented:
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.
woolmilkporcCommented:
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
TintinCommented:
Yes, that was my whole script.

So that is incorrect?   If so, could you please post the whole script.
labradorchikAuthor Commented:
Ok, sorry, I posted comment #37825517 in the wrong question!!
labradorchikAuthor Commented:
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

ozoCommented:
how are you running it?
Do you see anything if you run it verbose?
TintinCommented:
What output do you get when you do

bash -x scriptname
arnoldCommented:
You should enclose the filenames in "" this way if there are spaces in the name, it would not break the test.
simon3270Commented:
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.
labradorchikAuthor Commented:
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!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.