LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2011, 11:51 PM   #1
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Rep: Reputation: 148Reputation: 148
Read and execute set of linux commands from file


Following is the set of commands to execute one by one

commands.txt
Code:
/bin/date
/usr/bin/top -n 1 -b
/usr/bin/vmstat 1 3
/bin/df -h
/usr/bin/last | head -n 5
/bin/grep -i error /var/log/messages
/bin/grep -i fail /var/log/messages
/bin/grep -i panic /var/log/messages
/bin/grep -i warn /var/log/messages
/bin/grep 'su:' /var/log/messages | tail -10
/bin/grep telnet /var/log/messages | tail -10
/bin/grep ssh /var/log/messages | tail -10
/bin/grep ftp /var/log/messages | tail -10
/sbin/ifconfig
/bin/netstat -i 1 4
/usr/local/bin/iostat
/bin/grep root /etc/shadow
/bin/netstat -nr
shell script to run the above commands one by one
check.sh
Code:
#!/bin/bash
IFS=""
for line in `cat /home/ram/health-check/commands.txt`;do
sh -c $line >> /home/ram/health-check/health-check-$HOSTNAME-`date +%m%d%Y-%H%M`.txt
echo " "
done
But the above one return some error as follows
1. top: unknown argument '
2. /bin/df: invalid option
3. tail: invalid option -- 1
4. sh: line 14: /usr/local/bin/iostat: No such file or directory
5. /bin/netstat: invalid option --

I made this script as this also
Code:
run () {
while read -r x
do
echo -e "===================================================================================================================\n$x\n"
eval $x
echo " "
done < /home/ram/health-check/commands.txt
}
id=`id`
if [ ${id:0:5} = "uid=0" ]
then
run >> /home/ram/health-check/health-check-$HOSTNAME-`date +%m%d%Y-%H%M`.txt
else
echo "User must be root"
fi
echo "Output file created"
But no luck i couldn't success this script but same thing working in my Solaris environment.
 
Old 09-07-2011, 12:08 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I think you are being bitten by white space/new line problems. What happens if you try the following?
Code:
while read line ; do
  $line >> somefile.log
done < /home/ram/health-check/commands.txt
HTH,

Evo2.

Last edited by evo2; 09-07-2011 at 12:11 AM. Reason: Simplify
 
Old 09-07-2011, 12:26 AM   #3
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
Thanks Evo...
Umm.. the syntax is similar and no success still same error return
 
Old 09-07-2011, 02:23 AM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Adding carriage return characters to some of your commands gives error messages similar to yours.
Does the command file have Windows line endings '\r\n' instead of Linux '\n'?
Code:
# $'\r' creates a carriage return character in bash

/usr/bin/top -n 1 -b$'\r'
'       top: unknown argument '

/bin/df -h$'\r'
/bin/df: invalid option --

/bin/netstat -nr$'\r'
/bin/netstat: invalid option --
 
Old 09-07-2011, 02:59 AM   #5
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
when i run all these commands one by one from a text file, its skip the arguments
Ex:-
/bin/grep -i error /var/log/messages --> its just only execute the "grep" command and return error no argument passed to this grep
Code:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
 
Old 09-07-2011, 03:54 AM   #6
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
Thanks Kenhelm... Yup It caused because of windows file. now converted it as linux file (with dos2unix help)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to execute set of commands in procmailrc without redirecting mail to them golden_boy615 Linux - General 0 05-02-2011 06:35 AM
how to use expect - send to execute list of commands inside a file auma78 Linux - General 1 02-02-2011 01:00 PM
how to create execute file for linux telnet commands siva karthik Programming 7 03-10-2006 01:50 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM
How to set permissions to read, execute, write for users only chupacabra Linux - General 3 12-18-2002 04:54 PM


All times are GMT -5. The time now is 12:22 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration