- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- cat /file to varaible
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2010 10:04 PM
тАО04-01-2010 10:04 PM
cat /somefile.log to a string in Unix shell ?
Regards
Peter
Solved! Go to Solution.
- Tags:
- command substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2010 11:05 PM
тАО04-01-2010 11:05 PM
SolutionFor 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 12:16 AM
тАО04-02-2010 12:16 AM
Re: cat /file to varaible
typeset -i b=0
while read a
do
b=b+1
echo line "$b:$a"
done
or
exec 3
do
b=b+1
echo line "$b:$a"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 02:26 AM - edited тАО08-21-2011 03:58 PM
тАО04-02-2010 02:26 AM - edited тАО08-21-2011 03:58 PM
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<&-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 02:55 AM
тАО04-02-2010 02:55 AM
Re: cat /file to varaible
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 03:22 AM
тАО04-02-2010 03:22 AM
Re: 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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 03:53 AM
тАО04-02-2010 03:53 AM
Re: cat /file to varaible
>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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 02:06 PM
тАО04-02-2010 02:06 PM
Re: cat /file to varaible
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 03:50 PM
тАО04-02-2010 03:50 PM
Re: cat /file to varaible
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
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2010 03:54 PM
тАО04-02-2010 03:54 PM
Re: cat /file to varaible
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.