bash compare two numbers for change over a time period [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What I'm wondering is this. I get barometric readings that I want to create an alert for. I have a working one, but I would like to add a time comparison to be more accurate.
Example would be if "X" number falls below "X" number in lets say 5 minutes then do something.
Thanks for listening.

You could do something like below :
#!/bin/bash
while [ 0 ]
do
oldval=`cat reading` # Suppose that reading is the file where readings are updated
sleep 5m # sleeps for 5 minutes
newval=`cat reading`
if (( $newval < $oldval-5 ))
then
echo "$( date ) : Beep Beep " | tee -a barrolog
#above steps prints the output as well as append it to a log file
else
echo "$( date ) : No change " | tee -a barrolog
fi
done
# The script never reaches this point.
The script checks the reading relative to the previous one. You may wish to compare the readings against fixed value.

Related

Check if user matched [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need somthing like : user=[ "jack" "david" "root"] then check if `$1="david" do something.
Here is a quick and easy way to implement want you want in bash
declare -a USERS=("jack" "david" "root")
for i in "${USERS[#]}"
do
echo "current array entry: $i"
if [ "$i" == "$1" ]; then
echo "Found a match"
# do something
fi
done
Hope that helped
Since you are also gave the zsh tag, here is a zsh solution:
user=( jack david root )
if (( ${user[(Ie)$1]} > 0 ))
then
# $1 is in the user list
fi
${user[(Ie)$1]} calculates the position of $1 in the user array, and results in 0 if the user is not present.

How to remove last six characters in filename of 50 files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have some 50 files in D:\dummy\ folder. It has filenames like
Abc_566_1.xml.error
Abc_566_2.xml.error
...
Abc&566_50.xml.error
I want a shell script/solution to remove .error in all 50 filenames.
The shell can do quite an amount of string processing, like chopping off substrings at either end of a string. I usually do jobs like yours with
for file in *.error; do
mv "$file" "${file%.error}"
done
For more on this, read your shell manual, especially the section on parameter expansion.
Give this a try:
for each in *.error ; do
mv $each `echo $each | sed -e 's/\.error$//'`
done

Bash scripting and linuxbash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I hope someone can help me. my question is with a script in bash that receives a certain amount of parameters and then show them in the reverse order of the one. as I keep name one for each line in the vvariable and then show them around.
#!/bin/bash
var=""
for i in "$#";do
var+=`echo $i`
done
If you want reversed order, you have to loop in reversed order:
for ((i=$#; i>=1;i--)); do
a=${!i}
echo "$a"
done
The simplest way to achieve this would be:
echo $# | rev
$# stores all the arguments passed, and rev, as its name suggests, reverses the order of characters in the line.
Edit:
After reading your comment, I can suggest the following approach:
for i in `echo $# | rev`; do
j=`echo $i | rev`
echo -n "$j "
done

calculate difference between dates in shell script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to calculate the difference between two dates in shell script and if the result is greater than three months then it should throw an error to enter the correct start_date.
For example consider: start_date="2016-02-15", end_date=date +%Y-%m-%d
Thanks.
It isn't an perfect solution because it assumes that each month has 30 days, but it good point to start.
#!/bin/sh
start_date="2016-02-15"
end_date=$(date +%Y-%m-%d)
start_date_int=$(date -ud "${start_date}" +'%s')
end_date_int=$(date -ud "${end_date}" +'%s')
seconds=$(( ${end_date_int} - ${start_date_int} ))
days=$(( ${seconds} / 86400 )) # 60*60*24
months=$(( ${days} / 30 ))
if [ "${months}" -ge 3 ]; then
# is greater than 3 or equal 3
echo "error"
fi

Grep in file for good or wrong result and export those to values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have file with positive and negative values.
Want to export variable if everything is different from STATE_ACTIVE to be w1=STATE_INACTIVE if everything is STATE_ACTIVE to be w1=STATE_ACTIVE
STATE_ACTIVE
STATE_ACTIVE
STATE_INACTIVE
STATE_ACTIVE
I try with if operators but cannot able to catch the whole file values.
grep has a useful feature - $? is set based on whether it found anything or not.
grep -vq 'STATE_ACTIVE' myfile
if [ $? -eq 0 ]
then
export w1=STATE_INACTIVE
else
export w1=STATE_ACTIVE
fi
just grep -q 'STATE_INACTIVE' then read return code: $? if not zero, it means no INACTIVE found. otherwise, INACTIVE was hit.
-q makes grep don't output things.

Resources