unable to write a command to a file - bash

Unable to send an echo statement to a file , any help ? Currently I see the o/p coming for code free -k but none of the echo statements are coming .
I have tested with multiple echo statements and none are working.
#!/bin/bash
#--------Check for Memory Utilization--------#
set -x
ScriptName="${0##*/}"
LogTime=$(date "+%Y-%m-%d %H:%M:%S")
LogDate=$(date "+%Y-%m-%d")
FinalLogName="$ScriptName"_"$LogDate".log
touch "$FinalLogName"
echo "running test log" > "$FinalLogName"
echo "running testing log at $LogTime" > "$FinalLogName"
echo "running for script $ScriptName " > "$FinalLogName"
echo "running the script $ScriptName at $LogTime" > "$FinalLogName"
free -k > /tmp/memutil.log| tee "$FinalLogName" ; sed -n -e 2,3p -e 4p /tmp/memutil.log| tee "$FinalLogName"
rm /tmp/memutil.log
I expect the output to print all the echo statements with free -k command info.

Redirecting with > overwrites the file. And so does tee by default. To make an appending redirect use >> and the -a flag for tee.
#!/bin/bash
#--------Check for Memory Utilization--------#
set -x
ScriptName="${0##*/}"
LogTime=$(date "+%Y-%m-%d %H:%M:%S")
LogDate=$(date "+%Y-%m-%d")
FinalLogName="$ScriptName"_"$LogDate".log
touch "$FinalLogName"
echo "running test log" >> "$FinalLogName"
echo "running testing log at $LogTime" >> "$FinalLogName"
echo "running for script $ScriptName " >> "$FinalLogName"
echo "running the script $ScriptName at $LogTime" >> "$FinalLogName"
free -k > /tmp/memutil.log| tee -a "$FinalLogName" ; sed -n -e 2,3p -e 4p /tmp/memutil.log| tee -a "$FinalLogName"
rm /tmp/memutil.log

Related

Bash Script for using grep in an if statement with a for loop

I am trying to make my bash script ssh into each server and then grep Selinux=enforcing/replace with Selinux=permissive. The issue I am facing is it checks the first server and but not the second server. I believe it arises from my if statement.
#!/bin/bash
selinux_path=/opt/configtest
hosts=(server1 server2)
for my_hosts in "${hosts[#]}"
do
ssh -q -o "StrictHostKeyChecking no" root#${my_hosts} "
if [ $(grep -c SELINUX=enforcing $selinux_path) -ne 0 ]
then
echo "------------------------------------------------"
echo "${my_hosts}"
echo "------------------------------------------------"
sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' ${selinux_path}
echo "Selinux has been changed to permissive"
cat ${selinux_path}
else
echo "------------------------------------------------"
echo "${my_hosts}"
echo "------------------------------------------------"
echo "Selinux has already been changed to permissive"
cat ${selinux_path}
fi
"
done
You can't nest " inside ". If you want to give multiline input to ssh, the easiest way is with a here-doc.
#!/bin/bash
selinux_path=/opt/configtest
hosts=(server1 server2)
for my_hosts in "${hosts[#]}"
do
ssh -q -o "StrictHostKeyChecking no" root#${my_hosts} <<EOF
if grep -q SELINUX=enforcing "$selinux_path"
then
echo "------------------------------------------------"
echo "${my_hosts}"
echo "------------------------------------------------"
sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' ${selinux_path}
echo "Selinux has been changed to permissive"
cat "${selinux_path}"
else
echo "------------------------------------------------"
echo "${my_hosts}"
echo "------------------------------------------------"
echo "Selinux has already been changed to permissive"
cat "${selinux_path}"
fi
EOF
done
Did you try to specify the full path for grep, echo, sed and cat?

Bash Curl Loop Repeation

I have this question, why my context in file is not being submit properly from bash script, I just want to post my context in file to server via my parameters.
Error: My complete directory listings get posted into server.
#!/bin/bash
rm cookiestore.txt
rm output.txt
echo -e ""
echo -e "[-] Input Setting "
echo -ne "[+] Enter Host : "
read Host
echo -e "[?] Obtaining Cookie "
curl --silent --cookie-jar cookiestore.txt $Host > /dev/null
echo -ne "[+] Enter Parameters : "
read Parameters
echo -ne "[+] Enter File Path : "
read Filepath
echo -e ""
echo -e "[-] Performing Request "
echo -ne "[?] Checking Response URL : "
curl -sI $Host -w '%{response_code}%{redirect_url}\n' -o /dev/null
echo -ne "[?] Final Redirected Url : "
curl -Ls $Host -o /dev/null -w '%{url_effective}'
echo -e ""
echo -e ""
echo -e "[-] Posting "
echo -e "[+] Output file : output.txt"
echo -e ""
while read query
do
content=$(curl -L -X POST $Host --data "{$Parameters${query}}")
echo $query
echo $content >> output.txt
done < query.txt
echo -e ""
echo -e "[-] Finished "
echo -e ""

i can't run bash script in other script

This is my script in bash which I added to user crontab, and the command bash /home/lobby/start.sh doesn't work. Can someone help me with this?
#!/bin/bash
if ps aux | grep lobby | grep "Ss" | grep "SCREEN"; then
pkill -f lobby
screen -wipe
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer dzialal Uruchomiono pomyslnie" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer dzialal blad podczas uruchamiania" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
else
if bash /home/lobby/start.sh; then
echo "$(date '+%y-%m-%d') Serwer nie dzialal uruchomilem" >> /home/logi-serwery/logi-autorestart-lobby.txt
bash /home/lobby/start.sh;
else
echo "$(date '+%y-%m-%d') Serwer nie dzialal blad podczas uruchomienia" >> /home/logi-serwery/logi-autorestart-lobby.txt
fi
fi

Suggestions how to check for presence of a audio CD-ROM before continuing the script

I have found a nice solution to check if an audio cd is present. It works with a While loop which I like to end after the audio rip is done and the disc is ejected.
while true; do
sleep 10
cdparanoia -Q 2>&1 | grep 'audio only'
RET=$?
if [ $RET = "0" ] ; then
abcde -c ~/Music/abcde.conf -o flac,mp3,ogg -p -x
fi
done
I believe I found a solution to my wishes, hereby my code
#!/bin/bash
echo "voer componist in"
read componist
echo ingevoerde "$componist"
echo "voer het aantal CD's in"
read aantaldiscs
echo aantal discs $aantaldiscs
mkdir ~/Music/"$componist"
for a in $(seq -f %02g 01 $aantaldiscs);
do
mkdir ~/Music/"$componist"/$a
echo "OUTPUTDIR='~/Music/"$componist"/$a'" > ~/Music/abcde.conf
echo "OUTPUTFORMAT='\${TRACKNUM}.\${TRACKFILE}'" >> ~/Music/abcde.conf
echo "LAMEOPTS='-V 0'" >> ~/Music/abcde.conf
echo "FLACOPTS='-f --verify --best'" >> ~/Music/abcde.conf
echo "OGGENCOPTS='-q 8'" >> ~/Music/abcde.conf
echo "MAXPROCS=2" >> ~/Music/abcde.conf
echo "mungefilename ()" >> ~/Music/abcde.conf
echo "{" >> ~/Music/abcde.conf
echo "echo "$#" | sed s,:,\ -,g | tr / _ | tr -d \’\"\?\[:cntrl:\]" >> ~/Music/abcde.conf
echo "}" >> ~/Music/abcde.conf
while true; do
sleep 10
cdparanoia -Q 2>&1 | grep 'audio only'
RET=$?
if [ $RET = "0" ] ; then
abcde -c ~/Music/abcde.conf -o flac,mp3,ogg -p -x
break
fi
done
done

Shell script for generating HTML out put

The following script is generating the desired out put but not redirecting the result to /home/myuser/slavedelay.html
#!/bin/bash
host=<ip>
echo $host
user=usr1
password=mypass
threshold=300
statusok=OK
statuscritical=CRITICAL
for i in ert7 ert9
do
echo "<html>" > /home/myuser/slavedelay.html
if [ "$i" == "ert7" ]; then
slvdelay=`mysql -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
if [ $slvdelay -ge $threshold ]; then
echo "<tr><td>$i</td><td>CRITICAL</td>" >> /home/myuser/slavedelay.html
echo "<tr><td>$i</td><td>CRITICAL</td>"
else
echo "<tr><td>$i</td><td>OK</td>" >> /home/myuser/slavedelay.html
echo "<tr><td>$i</td><td>OK</td>"
fi
fi
done
echo "</html>" >> /home/myuser/slavedelay.html
If I cat the output file /home/myuser/slavedelay.html it gives.
<html>
</html>
Execution result :
sh slave_delay.sh
<tr><td>sdb7</td><td>OK</td>
Each time through the loop you're emptying the output file because of the command
echo "<html>" > /home/myuser/slavedelay.html
So the first iteration writes the <tr> row to the file, then the next iteration overwrites the file and doesn't write those lines because $i isn't ert7.
Change it to:
for i in ert7 ert9
do
if [ "$i" == "ert7" ]; then
echo "<html>" > /home/myuser/slavedelay.html
slvdelay=`mysql -u$user -p$password -h<ip> -S /backup/mysql/mysql.sock -e 'show slave status\G' | grep Seconds_Behind_Master | sed -e 's/ *Seconds_Behind_Master: //'`
if [ $slvdelay -ge $threshold ]; then
echo "<tr><td>$i</td><td>CRITICAL</td>"
else
echo "<tr><td>$i</td><td>OK</td>"
fi | tee -a /home/myuser/slavedelay.html
echo "</html>" >> /home/myuser/slavedelay.html
fi
done
Replace :
if [ "$i" == "ert7" ];
with:
if [ "$i" = "ert7" ];
You use = operator in test also.

Resources