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
683 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 76

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now