calculate difference between dates in shell script [closed] - shell

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

Related

How to write to file slash divided string word by word in Bash [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 1 year ago.
Improve this question
I have a string, for example /sd1/sd2/sd3/
I want to write it to a file in the next way
/sd1
/sd1/sd2
/sd1/sd2/sd3
How could I do that?
Note: the length of the string could be different. i.e. /sd1/ or /sd1/sd2/sd3/sd4/sd5/
This solution works for me
string="/sd1/sd2/sd3/"
IFS='/' read -r -a sds <<< "$string"
tmpsd=""
for (( i=1; i<${#sds[#]}; i++ )); do
tmpsd="${tmpsd}/${sds[i]}"
echo ${tmpsd} >> file.txt
done

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.

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

bash compare two numbers for change over a time period [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
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.

bash syntax for string and number comparision [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why does bash use -lt, -eq
etc for number comparison, and ==, != etc for string comparison ?
Bash syntax has its quirks which make it difficult to write and remember, but this seems completely non-intuitive for a simple use case.
Source : [bash syntax][1]http://www.ibm.com/developerworks/library/l-bash-test/index.html
bash only has one type: strings. More richly typed languages can overload (e.g.) the > operator to perform string comparison or numerical comparison based on the types of the arguments. Lacking anything other than a string type, bash must have separate operators for the different operations. Compare
[[ 9 > 10 ]] # exit status 0; 9 is lexicographically greater than 10
[[ 9 -gt 10 ]] # exit status 1; 9 is not numerically greater than 10
You can, however, use the normal operators for numbers if you use them inside an arithmetic expression, where bash assumes all values are either numbers or variables with numerical values.
(( 9 > 10 )) # exit status 1, 9 is not numerically greater than 10
(( 9 > foo )) # same as [[ 9 -gt $foo ]]

Resources