Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
699 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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 1000 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 20

Assisted Solution

by:simon3270
simon3270 earned 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

670 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