LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Read and execute set of linux commands from file (https://www.linuxquestions.org/questions/linux-newbie-8/read-and-execute-set-of-linux-commands-from-file-901507/)

kirukan 09-06-2011 10:51 PM

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.

evo2 09-06-2011 11:08 PM

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.

kirukan 09-06-2011 11:26 PM

Thanks Evo...
Umm.. the syntax is similar and no success still same error return

Kenhelm 09-07-2011 01:23 AM

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 --


kirukan 09-07-2011 01:59 AM

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.


kirukan 09-07-2011 02:54 AM

Thanks Kenhelm... Yup It caused because of windows file. now converted it as linux file (with dos2unix help)


All times are GMT -5. The time now is 02:06 AM.