- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script: Checking if a particular string ...
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
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
тАО06-22-2003 12:50 AM
тАО06-22-2003 12:50 AM
I would like to check if a particular string exists in a file, and if it doesn't, then insert that string in the file.
I was wondering if anyone could show me how I could perform such checks in a SHELL script?
I tried the following method below but it did not work:
#!/bin/sh
if [grep "pglcls01" syslog.conf|wc -l gt 1];then
echo Here
else
echo "echo "my string here" >> /home/file1
fi
duran% ./sys-1
./sys-1: [grep: command not found
wc: gt: No such file or directory
wc: 1]: No such file or directory
0 total
duran% ls -l syslog*
-rw-r----- 1 premir eng 390 Jun 17 17:38 syslog.conf
Also, how what is the syntax to to check if a value is greater than or equal (>=) in SHELL script?
Could some kindly help me out?
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 01:19 AM
тАО06-22-2003 01:19 AM
SolutionThere are syntax errors in your script. I modified it to work.
#!/bin/sh
if [ $(grep "pglcls01" syslog.conf|wc -l) -gt 1 ]
then
echo Here
else
echo "echo "my string here"" >> /home/file1
fi
$(commmand) will evaluate the command. You could also use the back ticks (`grep pglcls01 syslog.conf`).
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 06:31 AM
тАО06-22-2003 06:31 AM
Re: Shell script: Checking if a particular string exists in a file
If you just want to check if a string exist in a file you can use the return code:
If grep
then
echo "String exist"
else
echo "String don't exist"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 08:38 AM
тАО06-22-2003 08:38 AM
Re: Shell script: Checking if a particular string exists in a file
At the end of the file?
In the beginning of the file?
Somewhere in the middle?
In the beginning of the line or at the end of the line?
finding if a string of text exists is easy. placing new text in a flat file is also very straitforward, just let me know the answers to these questions and I'll write you a routine - do you prefer perl or shell scripts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 11:12 AM
тАО06-22-2003 11:12 AM
Re: Shell script: Checking if a particular string exists in a file
Try the next in csh:
grep -q "String"
if ($status == 0) then
echo "String exist"
else
echo "String" >>
endif
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 04:44 PM
тАО06-22-2003 04:44 PM
Re: Shell script: Checking if a particular string exists in a file
if grep "String" file >/dev/null 2>&1
then
echo "String exists!"
else
echo "String" >>file
fi
To check if a value is greater than or equal, you can use:
if [ ! $a -lt 10 ]
Good luck!
-ux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 07:52 PM
тАО06-22-2003 07:52 PM
Re: Shell script: Checking if a particular string exists in a file
I would like to have the string preferably at the beginning of the file.
If possible, would like to have the script to written in SHELL.
Thanks very much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 10:18 PM
тАО06-22-2003 10:18 PM
Re: Shell script: Checking if a particular string exists in a file
if ...
then
...
else
echo "String" >$$.tmp
cat file >>$$.tmp
cat $$.tmp >file
rm $$.tmp
fi
Good luck!
-ux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 10:20 PM
тАО06-22-2003 10:20 PM
Re: Shell script: Checking if a particular string exists in a file
So it could b :
if grep -q "pglcls01" syslog.conf
then
echo Here
else
(
echo "Mystring"
cat /home/file1
) > /tmp/tmp$$
mv /home/file1 /home/file1.prev
mv /tmp/tmp$$ /home/file1
fi
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-22-2003 10:42 PM
тАО06-22-2003 10:42 PM
Re: Shell script: Checking if a particular string exists in a file
CHK=`cat /etc/syslog.conf | grep pglcls01| wc -l`
if [ $CHK != 0 ]
then
echo "required string exists"
else
echo pglcls01 >> /etc/syslog.conf
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 12:38 AM
тАО06-23-2003 12:38 AM
Re: Shell script: Checking if a particular string exists in a file
Compleating my previous example. The string is added to the beginning of the file if it not exists.
If ! grep -q "string"
then
echo "string" >tmpfile
cat
mv tmpfile
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 08:57 AM
тАО06-23-2003 08:57 AM
Re: Shell script: Checking if a particular string exists in a file
#!/bin/sh
if grep "pglcls01" syslog.conf >> /dev/null; then
echo Here
else
echo "echo \"my string here\"" >> /home/file1
fi
or another way...
#!/bin/sh
y=`grep "pglcls01" syslog.conf|wc -l`
if [ $y -gt 1 ]; then
echo Here
else
echo "echo \"my string here\"" >> /home/file1
fi
Also, how what is the syntax to to check if a value is greater than or equal (>=) in SHELL script?
ans: -ge
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-25-2003 10:22 AM
тАО06-25-2003 10:22 AM
Re: Shell script: Checking if a particular string exists in a file
Return to my option:
grep -q "String"
if ($status == 0) then
echo "String" > /tmp/temp.$$
cat LOGFILE >> /tmp/temp.$$
mv -f /tmp/temp.$$ LOGFILE
endif
Caesar