I am a Bash noob and would like to learn how to do this. Objective: Write a bash script, that runs commands that are listed in a .txt file, check if the command worked, if yes output in a file, if no skip this command.
I have a folder named /scripts and inside this folder I have 2 files commands.txt and compiler.sh
commands.txt has the the commands that I would like to run on my script. Below is what I currently have saved on this file.
#ServerInformation
hostname
lscpu
lshw
hwinfo
lspci
lscsi
lsusb
inxi
lsblk
df
fdisk -l
mount | column -t
free -m
dmidecode
sudo cat /proc/cpuinfo
sudo cat /proc/meminfo
fakecommand
#NetworkInformation
ifconfig
and here is compiler.sh
#!/bin/bash
cat commands.txt | while read -r line
do
echo "Running Command $line"
"$line"
if [ "$?" -ne 0 ]
then
echo "The command $line does not exist. Skipping."
else
echo "$line" >> datacollect.txt
"$line" >> datacollect.txt
echo ""
echo ""
fi
done
I am having problems with the command that uses "Cat" as I am getting this error: compiler.sh: 5: compiler.sh: sudo cat /proc/cpuinfo: not found
If anyone knows, how do I get around this problem? also why is this problem happening?
Thank you in advance!
Edit I found that fdisk -l is also getting this problem. so not only the Cat commands. Could the "space" be the problem?
Thank you!