The grep command's quiet flag is being ignored - bash

$ sudo service --status-all | grep -q oijawpawuouzmaomwaoiwadlknasdlknascsc &>/dev/null
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] plymouth
[ ? ] plymouth-log
$ sudo service --status-all
[ + ] apache-htcacheclean
[ + ] apache2
[ - ] apparmor
[ + ] awslogs
[ - ] console-setup.sh
[ - ] cron
[ - ] dbus
[ ? ] hwclock.sh
[ + ] irqbalance
[ - ] keyboard-setup.sh
[ ? ] kmod
[ + ] php7.2-fpm
[ ? ] plymouth
[ ? ] plymouth-log
[ - ] procps
[ - ] rsync
[ - ] rsyslog
[ + ] udev
[ - ] ufw
[ - ] unattended-upgrades
[ - ] uuidd
[ - ] x11-common
Wondering why the output from grep with the quiet flag is appearing, sending outputs to dev null is also failing.
I'm also confused as to why the gibberish is matching against anything.

Related

Shell can't open libs/init another shell script

I have a shell script, that should import another shell script:
#!/bin/sh
. libs/init
However it complains:
/sh/main.sh: 3: .: Can't open libs/init
the folder is structured as follows:
the content of the init.sh file:
#!/bin/sh
echo "<===========================================>"
echo "Check if all variables are set"
echo "<===========================================>"
[ -z "$DO_AC" ] && exit 1
[ -z "$K8S_CLUSTER" ] && exit 1
[ -z "$ARGO_SERVER" ] && exit 1
[ -z "$ARGO_USERNAME" ] && exit 1
[ -z "$ARGO_PW" ] && exit 1
[ -z "$IMAGE_URL" ] && exit 1
[ -z "$IMAGE_VERSION" ] && exit 1
[ -z "$CHARTS_URL" ] && exit 1
[ -z "$GITOPS_URL" ] && exit 1
[ -z "$APP_TYPE" ] && exit 1
[ -z "$APP" ] && exit 1
[ -z "$APP_URL" ] && exit 1
[ -z "$GIT_USER" ] && exit 1
[ -z "$GIT_AT" ] && exit 1
[ -z "$APP_TLS_SECRET" ] && exit 1
echo "All variables are set"
What am I doing wrong?
chmod u+r init.sh
That will make sure you've got read access to the file, which that error message suggested to me, might be your problem. Permissions sometimes get changed for screwy reasons; as long as you're not randomly setting things suid root, however, it's not too much of a disaster.

Transforming IF statements to FOR or WHILE (Shell Script)

The following code is displaying a Progress Bar on an HTML page. It runs fine but takes up far to many lines of code.
QUESTION:
- How would I go about transforming this into a for or while loop, replicating its functionality?
if [ $per_usage -ge 1 ] && [ $per_usage -le 10 ]
then
indg="|"
indb=""
indr=""
elif [ $per_usage -gt 10 ] && [ $per_usage -le 20 ]
then
indg="||"
indb=""
indr=""
elif [ $per_usage -gt 20 ] && [ $per_usage -le 30 ]
then
indg="|||"
indb=""
indr=""
elif [ $per_usage -gt 30 ] && [ $per_usage -le 40 ]
then
indg="||||"
indb=""
indr=""
elif [ $per_usage -gt 40 ] && [ $per_usage -le 50 ]
then
indg="|||||"
indb=""
indr=""
elif [ $per_usage -gt 50 ] && [ $per_usage -le 60 ]
then
indg="|||||"
indb="|"
indr=""
elif [ $per_usage -gt 60 ] && [ $per_usage -le 70 ]
then
indg="|||||"
indb="||"
indr=""
elif [ $per_usage -gt 70 ] && [ $per_usage -le 80 ]
then
indg="|||||"
indb="|||"
indr=""
elif [ $per_usage -gt 80 ] && [ $per_usage -le 90 ]
then
indg="|||||"
indb="||"
indr="||"
elif [ $per_usage -gt 90 ]
then
indg=""
indb=""
indr="||||||||||"
else
indg=""
indb=""
indr=""
fi
For example my output is like if per_usage value is 41
41 % |||||
Thank you in advance.
This sort of thing can be looped pretty easily:
#!/bin/bash
get_string () {
per_usage="$1"
if [ "$per_usage" -le 100 ] && [ "$per_usage" -ge 0 ]; then
echo -en "${per_usage}%\t"
bars=$(($per_usage / 10 + 1))
printf "%0.s|" $(seq 1 $bars)
echo
fi
}
i=0
while [ "$i" -le 100 ]; do
string=$(get_string "$i")
echo "$string"
let i++
done
In this example, the get_string function can be used to generate a string based on an input number. For instance, get_string 41 will print 41% |||||. In the little while loop below, the string for 0 - 100 is stored in the $string variable and printed.
bars stores the number of bars to print. Just one bar per 10%. Then printf and seq are used to print a | bars number of times.
Hopefully with this, you'll be on the right track to cleaning up your code.

How to write a script with multiple pings as condition to run a command?

I need to be sure that 2 machines are functional and 1 machine is sleeping to run my command. Here the logic:
#!/bin/bash
ping1=$(192.168.2.15)
ping2=$(192.168.2.15)
ping3=$(192.168.2.21)
if [ $ping1 = OK ] && [ $ping2 = OK ] && [ $ping3 = failure ]
then run_command
elif [ ping1 = OK ] && [ ping2 = OK ] && [ ping3 = OK ]
then echo 'machine 3 already running'
fi
I know the syntax is bad, because i don't know what do i have to write in the [ ]?
Shell commands have a return status accessible by $?, ping will return 0 if it was successful, and something else instead, so we might as well test on that.
We can retrieve the status this way :
ping -c 1 192.168.2.15; ping1=$?
ping -c 1 192.168.2.17; ping2=$?
ping -c 1 192.168.2.21; ping3=$?
where -c 1 will tell ping to only send one request.
Then we can test the values using numerical comparison :
if [ $ping1 -eq 0 ] && [ $ping2 -eq 0 ] && [ $ping3 -ne 0 ]
By the way, I would factor your tests into this form :
if ping1 && ping2 then
if ping3 then
else #(if ! ping3)
fi
fi

bash if-statement check for file

I know how to check for a file in bash using this code
file=$1
if [ -f "$file" ]
then
...
fi
But I want to do something when it's not a file.
file=$3
if [ "$1" == "" ] || [ "$2" == "" ] || [ $file is not a file??? ]
then
echo "use: notEmpty notEmpty file"
fi
Can anyone help me out?
if [ "$1" == "" ] || [ "$2" == "" ] || [ ! -f "$file" ]
The whitespaces after [ and before ] are important.

Shell Script Error

Can any one tell what wrong with this script ?. Because I am getting the error like
./timer: line 9: [13: command not found
./timer: line 12: [13: command not found
My script look like
#!/bin/bash
while :
do
HOUR=$(date +%H)
MINUTE=$(date +%M)
SECOND=$(date +%S)
if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]
then ./binary
elif [$HOUR == 18] && [$MINUTE == 30] && [$SECOND == 1]
then ./binary
fi
done
put a space between the [ ... ]
Example:
if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]
Should become
if [ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]
I think you must use "${VARIABLE}" and respect spaces for square brackets
This would give :
if [ "${HOUR}" == 13 ] && [ "${HOUR}" == 12 ] && [ "${HOUR}" == 1 ]
Hope that helps !
the test operators in bash need to have a space by the opening and closing bracket, try
[ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]
and
[ $HOUR == 18 ] && [ $MINUTE == 30 ] && [ $SECOND == 1 ]
here is a link that you might find useful
http://tldp.org/LDP/abs/html/testconstructs.html
Several things:
You have to put a space after [ and before ].
It's a good practice to protect your variables using double quotes.
You have to use the -eq operator to compare numeric values (see Bash conditional operators).
Like so:
if [ "$HOUR" -eq 13] && [ "$MINUTE" -eq 12 ] && [ "$SECOND" -eq 1 ]
elif [ "$HOUR" -eq 18 ] && [ "$MINUTE" -eq 30 ] && [ "$SECOND" -eq 1 ]

Resources