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

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?

Related

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

unable to write a command to a file

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

How to suppress this sed in Bash?

#!/bin/bash
set_bash_profile()
{
local bash_profile="$HOME/.profile"
if [[ -w $bash_profile ]]; then
if (grep 'MY_VAR' $bash_profile 2>&1); then
sed -i '/MY_VAR/d' $bash_profile
fi
echo "export MY_VAR=foo" >>$bash_profile
fi
}
set_bash_profile
Here is the first run:
bash-4.1$ ./set_bash.sh
No output --which is great! And cat shows export MY_VAR=foo was appended to the file. But when executing a second time, I want sed to silently edit $bash_profile without outputting the matching string, like it does here:
bash-4.1$ ./set_bash.sh
export MY_VAR=foo
You get the output from grep on grep 'MY_VAR' $bash_profile 2>&1. grep outputs the matched line in your profile:
export MY_VAR=foo
on stdout. The 2>&1 only forwards stderr to stdout. It's good to use -q option with grep. Also the subshell (...) around the grep is not needed. Try this:
#!/bin/bash
set_bash_profile()
{
local bash_profile="$HOME/.profile"
if [ -w $bash_profile ]; then
if grep -q 'MY_VAR' $bash_profile; then
sed -i '/MY_VAR/d' $bash_profile
fi
echo "export MY_VAR=foo" >>$bash_profile
fi
}
set_bash_profile

reading variables to ps ax script

Hi I've been searching on the forum but I cant seem to get this right. I am trying to create a script that asks the user which process they are searching for then returns with a 1 if the process is running.
This works:
#!/bin/bash
SERVICE='httpd'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi
I want to add this to the script:
echo -e "please enter process name: \c"
read word
for something like:
#!/bin/sh
echo -e "please enter process name: \c"
read input_variable
if ps ax | grep -v grep | grep $varname > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi
Use pgrep to search for processes:
read process_name
if pgrep "${process_name}" >/dev/null 2>&1 ; then
"echo ${process_name} found"
else
"echo ${process_name} not found"
fi

Save echo as output within script

The following is a little bit of my code:
for((a=1;a<=8000000;a++))
do
if (($a%4==0))
then
b=`cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c`
echo `cat 101127_2_aa_1.fastq|head -$(($a-3))|tail -1`
echo `cat 101127_2_aa_1.fastq|head -$(($a-2))|tail -1|cut -c 1-$(($b-1))`
echo `cat 101127_2_aa_1.fastq|head -$(($a-1))|tail -1`
echo `cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'`
fi
done
This if loop is "echo" the output; however I hope to save the echo output into some file. And I wanna manage this WITHIN script.
I mean probably sth. like:
`for((a=1;a<=8000000;a++))
do
if (($a%4==0))
then
b=`cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c`
echo `cat 101127_2_aa_1.fastq|head -$(($a-3))|tail -1`
echo `cat 101127_2_aa_1.fastq|head -$(($a-2))|tail -1|cut -c 1-$(($b-1))`
echo `cat 101127_2_aa_1.fastq|head -$(($a-1))|tail -1`
echo `cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'`
fi
done` > output
But obviously this doesn't work; and I'm asking for the right way to save echo output within script.
thx
Always a good practice to use $(…) instead of ``.
for((a=1;a<=8000000;a++))
do
if (($a%4==0))
then
b=$(cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c)
echo $(cat 101127_2_aa_1.fastq|head -$(($a-3))|tail -1)
echo $(cat 101127_2_aa_1.fastq|head -$(($a-2))|tail -1|cut -c 1-$(($b-1)))
echo $(cat 101127_2_aa_1.fastq|head -$(($a-1))|tail -1)
echo $(cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g')
fi
done >> output
Update:
As #Sorpigal suggested, this still uses cat command when we can do the same with head
You can append >> output to each of the echo lines.
Also the echo is redundant in your script. Instead of
echo `cat ...`
you can use
cat ...
Just remove backticks and keep redirect.
Alternatively you can
exec >output
before the for.

Resources