Why is the if-compression not comparing the value?
idle_time=exec sudo -u home xprintidle
if [ "$idle_time" -ge 6000 ]
then
echo "hi"
fi
it is not working like that
First of all, I changed to line which you use to execute xprintidle as a different user.
After that I adjusted the if clause, since there was an error as well.
#!/bin/bash
# actually assign the variable
idle_time=$(idle_time=exec sudo -u home xprintidle)
# adapted if clause to actually match (see https://stackoverflow.com/questions/18668556/comparing-numbers-in-bash)
if [ "$idle_time" -gt "6000" ]; then
echo "hi"
fi
Related
I'm currently writing a bash script which should install all the software that I need. The process looks promising so far: First, I write a "software-list.txt" file which contains all dependencies for multiple distros. Afterwards bash split these values into an array and reads the corresponding value out of it. Finally the script should combine the distro package manager name (e.g. dnf, if I'm using Fedora Linux) with the operator ("install") with the arguments (which are the software packages).One last info: All variable names which don't appear in the source code, we're defined beforehand
The script looks like this:
One last info: All variable names which don't appear in the source code, were defined beforehand
case "$DISTRO_NAME" in
"Fedora")
PROGRAMM="dnf"
CSV_INDEX=0;;
"Debian")
PROGRAMM="apt-get"
CSV_INDEX=1;;
esac
# Read all required packages
while IFS= read -r line
do
IFS=','
LINE=($line)
if [ $CURR_LINE_INDEX -gt 1 ] && [ $CURR_LINE_INDEX -lt $LINE_COUNT ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]} "
elif [ $CURR_LINE_INDEX -eq $LINE_COUNT ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]}"
fi
CURR_LINE_INDEX=$((CURR_LINE_INDEX+1))
done < "software-list.txt"
# Run installation script
$PROGRAMM $OPERATOR $ARGUMENTS
However, whenever I run the script, the command is correct. But the output is always the same "couldn't find any match for packagex packagey"
I followed Jetchisel's advice and did a shellcheck. How is it now?
#!/bin/bash
case "$DISTRO_NAME" in
"Fedora")
PROGRAMM="dnf"
CSV_INDEX=0;;
"Debian")
PROGRAMM="apt-get"
CSV_INDEX=1;;
esac
# Read all required packages
while IFS= read -r -a line
do
IFS=','
LINE=("${line[#]}")
if [ "$CURR_LINE_INDEX" -gt 1 ] && [ "$CURR_LINE_INDEX" -lt "$LINE_COUNT" ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]} "
elif [ "$CURR_LINE_INDEX" -eq "$LINE_COUNT" ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]}"
fi
CURR_LINE_INDEX=$((CURR_LINE_INDEX+1))
done < "software-list.txt"
# Run installation script
"$PROGRAMM $OPERATOR $ARGUMENTS"
apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;
My requirement is if I pass service name as a 'apps' then I need to clone the $apps url.
Here
if [ $Service -eq apps ]
not think a good approach as my repo url might get increased so more and more loop will come
Any suggestions?
The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.
#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;
#Store global
repo=''
# if empty parameter is passed
if [ $# -lt 1 ] ; then
echo "Parameters Need"
exit 1
fi;
# for search the correct parameter
if [ $1 = "apps" ]; then
repo=$apps
elif [ $1 = "cluster-ui" ] ; then
repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
repo=$customerui
else
echo "Not found"
fi;
echo $repo
Note just repeat elif [ ] ;then for more entries or think!
how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below
chmod 766 file
now run the shell script sh ./file.sh clusterui
'Case statement' would suit here more than if ladder
I have written a script to check process is running or not,it work fine but while testing it, i have found that it not include top command count running in other terminal
check-process.sh
#!/bin/sh
OK=1
CRITICAL=0
PROCESS_NUM=$( ps -ef | grep $1 | grep -v "grep "|grep -v "sh"|wc -l )
#echo $PROCESS_NUM
if [ $PROCESS_NUM = $OK ]
then
echo "OK"
elif [ $PROCESS_NUM = $CRITICAL ]
then
echo "CRITICAL"
elif [ $PROCESS_NUM > $OK ]
then
echo "MULTIPLE process are runing"
else
echo "error"
fi
And i run top command in two terminals and run this script as follow:
./check-process.sh top
and out put is 0 CRITICAL , but when i run normal command ps -ef |grep -v "grep "| wc -l it gives two counts.
That mess of greps just has to go.
One "trick" for finding processes by name without finding your grep is to use a regular expression. That is, after all, what the Global Regular Expression Print command is for. You can use parameter expansion to construct a safe regular expression based on your input string, perhaps like this:
#!/bin/sh
if [ -z "$1" ]; then
echo "I'd love me an option." >&2
exit 1
fi
OK=1
CRITICAL=0
x="${1#?}" # make a temporary string missing the 1st chararcter,
re="[${1%$x}]$x" # then place the 1st character within square brackets.
PROC_COUNT=$( ps -ef | grep -w -c "$re" ) # yay, just one pipe.
if [ "$PROC_COUNT" -eq "$OK" ]; then
echo "OK"
elif [ "$PROC_COUNT" -eq "$CRITICAL" ]; then
echo "CRITICAL"
elif [ "$PROC_COUNT" -gt "$OK" ]; then
echo "MULTIPLE process are running"
else
echo "error"
fi
There are a few notable changes here:
I added something to fail with better explanation if no option is given.
The pipeline, of course. And the lines that create $re.
We're using -gt and -eq to test numeric values. man test for details.
I renamed your count variable to be clearer. What is a "PROCESS_NUM" really? Sounds like a PID to me.
All variables are quoted. I don't need to tell you why, you have the Google.
That said, you should also consider using pgrep instead of any sort of counting pipe, if it's available on your system. Try running pgrep and see what your OS tells you.
cplane_pid=`pidof hnb_gw.exe`
if [ -z $cplane_pid ]
then
STATUS=`failure`
echo "Cplane hnbgw running $STATUS"
else
STATUS=`success`
echo "Cplane hnbgw running $STATUS"
fi
echo
If there are multiple instances of hnb_gw.exe, pidof will return multiple pids. The -z of [ expects only one pid. One solution might be to use the -s switch of pidof to return only one pid.
You need to Use More Quotes™:
if [ -z "$cplane_pid" ]
Adding set -x before and set +x after the command shows you what it results in. For example:
$ cplane_pid="1 2 3"
$ set -x
$ [ -z $cplane_pid ]
+ '[' -z 1 2 3 ']'
bash: [: too many arguments
In other words, each of the whitespace-separated values in the variable was used as a single parameter. Since -z requires exactly one parameter, this results in a syntax error.
Rather than saving this as a variable, you can simply do
if ! pidof hnb_gw.exe > /dev/null
If the process doesn't exist, it will return 1 ("false").
When you execute
cplane_pid=`pidof hnb_gw.exe`
then cplane_pid can contain more (space separated) items.
So the expansion in
if [ -z $cplane_pid ]
will become
if [ -z firstPid secondPid etc ]
and that is your error "[: too many arguments"
You can solve this with quoting the variable (you should do this ALWAYS in shell)
if [ -z "$cplane_pid" ]
or use [[ (if it's installed on your system), which is better in many ways. For instance you don't need to quote variable :)
if [[ -z $cplane_pid ]]
is the same as
if [[ -z "$cplane_pid" ]]
For testing purposes (and erros like this) use -x hasbang bash option
#!/bin/bash -x
or use debug sections
-- normal code --
set -x # debug section starts here
[ -z $cplane_pid ] && echo zero
eval something
set +x # debug section ends here
-- normal code --
also you can call the script
/bin/bash -x yourScript.sh
pidof can return more than one pid, in these cases, your test will get too many arguments.
We have code to check for a node install:
which="type -p"
if [ $SHELL = "/bin/zsh" ]; then
which="whence"
fi
# make sure that node exists
node=`$which node 2>&1`
ret=$?
if [ $ret -ne 0 ] || ! [ -x "$node" ]; then
<"This error code is returned">
But when I run this with ZSH (OhMyZsh) it returns a 127 (does not exist). Commenting out the which="whence" lets it run fine.
Without removing the whole aliasing bit is there any way to have ZSH play along with this? Ideally I'd like to make a change on my end to make this work rather than modifying this code at all.
You mean, you run $node and it appears that you’ve tried to run command whose name is node --alias-args which does not exist?
If this is true, change the third line to use whence -p: it has the same output as type -p in bash. If not, please, explain when this code is returned.
Update: I do not know what was done in ohmyzsh (though I have not a single idea how to make a builtin not found) so just try to rewrite the code in this way:
# At the very top
if [ -n $ZSH_VERSION ] ; then
emulate -L zsh
endif
<...>
which="type -p"
if [ -n $ZSH_VERSION ] ; then
which=( whence -p ) # Changes variable type as well
endif
node=`$which node 2>&1`
if (( ? )) || ! test -x $node ; then
<...>