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?
 
TintinConnect With a Mentor Commented:
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
 
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
...
0
 
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
woolmilkporcCommented:
You should omit the comment in line 6 ("# reading file1.dat"). Some shells dont like hash characters ("#") after "read".
0
 
woolmilkporcCommented:
... besides that I can't see anything unusual.

Did you post the whole script?
0
 
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.
0
 
woolmilkporcConnect With a Mentor Commented:
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
 
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?
0
 
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.
0
 
woolmilkporcCommented:
I fear I don't fully understand your previous comment #37825517 ...
0
 
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.
0
 
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
0
 
TintinCommented:
Yes, that was my whole script.

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

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

bash -x scriptname
0
 
arnoldCommented:
You should enclose the filenames in "" this way if there are spaces in the name, it would not break the test.
0
 
simon3270Connect With a Mentor Commented:
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
 
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!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.