1753742 Members
4751 Online
108799 Solutions
New Discussion юеВ

cat /file to varaible

 
SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

cat /file to varaible

Hi there I'm trying to crate a script which reads a log file content. Ho do I get an output of:

cat /somefile.log to a string in Unix shell ?

Regards
Peter
Jesus is the King
10 REPLIES 10
Matti_Kurkela
Honored Contributor
Solution

Re: cat /file to varaible

The output of any command (or a pipeline of commands) can be captured into a string by enclosing the command/pipeline with $( ).

For example, if you want to read the first line of a file to an environment variable, you might do something like this:

PIDVAR=$( head -1 /somewhere/file.pid )

However, if you simply want to read the entire contents of the file into a string, you can use $(< ). This is faster than using $(cat /somefile.log), as the shell does not have to fork a new "cat" process. If your script does something repeatedly in a tight loop, this might have a significant effect on performance.

Your example file could be read into a LOGVARIABLE by:

LOGVARIABLE=$(< /somefile.log )

MK
MK
Laurent Menase
Honored Contributor

Re: cat /file to varaible

if you want to read it line by line you can also do

typeset -i b=0
while read a
do
b=b+1
echo line "$b:$a"
done

or
exec 3while read -u3 a
do
b=b+1
echo line "$b:$a"
done
Dennis Handly
Acclaimed Contributor

Re: cat /file to variable

>Laurent: b=b+1

You'll need to use: (( b = b + 1 ))

Or: (( b += 1 ))


>exec 3< somefile.log

You should probably close it when you are done:
exec 3<&-

James R. Ferguson
Acclaimed Contributor

Re: cat /file to varaible

Hi Peter:

You can also create/capture a shell 'here-document' into a string should the need arise:

LIST=$(
while read LINE
do
echo ${LINE}
done <<- EOF!
file1
file2
file3
EOF!
)
echo ${LIST}

Regards!

...JRF...
Suraj K Sankari
Honored Contributor

Re: cat /file to varaible

Hi,

>>Hi there I'm trying to crate a script which reads a log file content. Ho do I get an output of:


you can try to grep the strings which you are searching in log.
like if i need to search EMS strings in /var/adm/syslog/syslog.log then I will do

grep -i ems /var/adm/syslog/syslog.log

Suraj
Laurent Menase
Honored Contributor

Re: cat /file to varaible

>>Laurent: b=b+1
>You'll need to use: (( b = b + 1 ))
No in ksh or posix shell since I typed b with
typeset -i b=0, I don't need to use
(( ))

# typeset -i b=0
# b=b+1
# echo $b
1
# b=b+1
# echo $b
2

>>exec 3< somefile.log

>You should probably close it when you are >done:
>exec 3<&-
indeed it is cleaner to close it once the loop read all the file so at loop exit.
Piotr Kirklewski
Super Advisor

Re: cat /file to varaible

Thanks for all the responses.
I got stock on the e-mail part. I was always using this:

echo "test" |mail -s "test" yyy@xxx.com

But this time I got the following error:
/usr/sbin/sendmail: Exec format error

If I try this:

cat /somefile.log << EOF | sendmail -t
to:xxx@yyy.com
from:xxx@yyy.com
subject:Testing 123
EOF

There is no error but I'm not getting the email either.


Please advice



Jesus is the King
Steven Schweda
Honored Contributor

Re: cat /file to varaible

> /usr/sbin/sendmail: Exec format error

uname -a
ls -l /usr/sbin/sendmail
file /usr/sbin/sendmail


Did "root" do something like
blah-blah-blah > /usr/sbin/sendmail
instead of:
blah-blah-blah | /usr/sbin/sendmail
?
Raj D.
Honored Contributor

Re: cat /file to varaible

Peter,

Instead of mail try mailx in hp-ux and it will work.


$ echo "test" |mailx -s "test-mail" yyy@xxx.com


More info :
http://docs.hp.com/en/5992-5835/ar01s04.html
( mailx example in : 2. b. )


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "