LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2008, 01:20 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
bash - add another file to variable


I have written a script that checks size of a file - if size is greater that
zero, it sends out an email with the content of the file. I want to add
another file to the script and wondering if it is possible. Here is my script

Code:
#!/bin/bash
FILE="/path/to/file1"
LS="`ls -al $FILE | awk '{print $5}'`"
if [ $LS = "0" ]; then
       exit 1;
       else
  cat $FILE | /mail -s "Subject" joe@domain.com
fi
I have added /path/to/file2 to the FILE variable, like, FILE="/path/to/file1" && "/path/to/file2" - but it says permission denied on file2.

Is there any way to add file2 to the FILE variable?

Thanks for any help.

Last edited by noir911; 08-13-2008 at 01:23 AM.
 
Old 08-13-2008, 02:04 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
In bash you have a test specifically to see if a file exists and has a size greater than 0 bytes: -s. There is no need to call ls and then awk to extract the size.

Here's how to do that:

Code:
#!/bin/bash

FILE=/path/to/a/file
if [ -s "$FILE" ]; then
    cat $FILE | mail ...
fi
Incidentally, you don't have to use the if ... then ... fi construct for this, it's much more compact and pretty to do it like this:
Code:
#!/bin//bash
FILE=/path/to/a/file
[ -s "$FILE" ] && cat "$FILE" | mail ...
Which style you use is a matter of taste really.

If you want to check multiple files, doing the same action on each, and do not much feel like copy-pasting the whole statement, you can use a function which takes a file name as an argument, and performs the test and mail action. Once you've written the function, you can call it with as many file names as you like:

Code:
#!/bin/bash

mail_if_non_zero_size () {
    [ -s "$1" ] && cat "$1" | mail ...
}

mail_if_non_zero_size "/path/to/some/file"
mail_if_non_zero_size "/path/to/a/second/file"
mail_if_non_zero_size "/path/to/yet/another/file"
The thing to know is that within a shell function, arguments which are passed when the function is called get assigned to the variables 1, 2, 3, 4, ... so you can take their value with $1, $2, $3, $4, ...

The -s test is one of many file-related tests. See the "CONDITIONAL EXPRESSIONS" section of the bash manual page for a full list.
 
Old 08-13-2008, 02:11 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
You could do it differently:
Code:
#!/bin/bash
FILES=(
  "/path/to/file1"
  "/path/to/file2"
  …
  "/path/to/fileN"
)
find "${FILES[@]}" -maxdepth 0 -type f ! -empty -print | while read f; do
  cat "$f" | /mail -s "Subject" joe@domain.com
done
Yves.
 
Old 08-13-2008, 02:11 PM   #4
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
matthewg42 and Theyinyeti are doing a great job explaining how you can do this differently...so I'll focus on your original question.

Quote:
I have added /path/to/file2 to the FILE variable, like, FILE="/path/to/file1" && "/path/to/file2" - but it says permission denied on file2.
What happened here is because FILE="/path/to/file1" was assigned successfully to FILE, therefore /path/to/file2 was executed. However, file2 probably didn't have execute rights, so you got the permission denied error.

In other words, you tried to "run" /path/to/file2, when all you want to do was append the text "/path/to/file2" to "/path/to/file1"

If you wanted to append to an existing variable, easiest way is:

FILE="$FILE /path/to/file2"

Hope this helps.
 
Old 08-13-2008, 02:15 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Having multiple file names in a single string value with whitespace separation is not a good idea - what happens when you get a filename with a space in it? It breaks. The reason I suggested an alternative is because the way you seem to want to do it is not robust.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH writing from variable to file Planescape Programming 6 01-11-2007 10:41 AM
Variable in Bash get from other file?? helptonewbie Programming 4 08-21-2006 07:51 AM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 01:41 PM
how to add environment variable to .bashrc file hasan Linux - General 7 10-03-2003 04:43 PM
how to add environment variable to .bashrc file? hasan Red Hat 1 10-03-2003 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration