- Weather
weather(){ curl -s "wttr.in/$1?m1"}
2. Timer
function count() {
total=$1
for ((i=total; i>0; i--)); do sleep 1; printf "Time remaining $i secs \r"; done
echo -e "\a"
}
3. Up
function up() {
times=$1
while [ "$times" -gt "0" ]; do
cd ..
times=$(($times - 1))
done
}
Needn’t do
cd ../../..
any longer. Just do up 3.
4. Program for Extract.
function extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.tar.xz) tar Jxvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip -d `echo $1 | sed 's/\(.*\)\.zip/\1/'` $1;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'" ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
Who
has time in the world to figure out the right extract commands for all
the possible formats.
5. Trap forced exit of bash script.
Don't let your script exist unexpectedly, trap when someone press ctrl+c and exit from your script gracefully.
bash script:
trap ctrl_c INT
function ctrl_c() {
echo "** Trapped CTRL-C"
}
echo "** Trapped CTRL-C"
}
for i in `seq 1 5`; do
sleep 1
echo -n "."
done
6. colorful banner with figlet and lolcat
apt-get install figlet lolcat
echo "test" | figlet
echo "testing" | lolcat
ll | lolcat
7. Get First day and last day of the month with using bash script.
#!/bin/bash
# last day for month
if [ ! -n "$1" ]; then
echo "please pass parameters."
echo "script.sh YYYY MM"
exit 0
fi
lastday() {
# ja fe ma ap ma jn jl ag se oc no de
mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31')
year=$1
month=$2
if [ $month -ne 2 ] ; then
echo ${mlength[$month]}
return 0
fi
leap=0
((!(year%100))) && { ((!(year%400))) && leap=1 ; } || { ((!(year%4))) && leap=1 ; }
feblength=28
((leap)) && feblength=29
echo $feblength
}
# date to Julian date
date2jd() {
year=$1
month=$2
day=$3
lday=$(lastday $year $month) || exit $?
if ((day<1 || day> lday)) ; then
echo day out of range
exit 1
fi
echo $(( jd = day - 32075
+ 1461 * (year + 4800 - (14 - month)/12)/4
+ 367 * (month - 2 + (14 - month)/12*12)/12
- 3 * ((year + 4900 - (14 - month)/12)/100)/4
- 2400001 ))
}
jd2dow()
{
days=('Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday')
jd=$1
if ((jd<1 || jd>782028)) ; then
echo julian day out of range
return 1
fi
((dow=(jd+3)%7))
echo ${days[dow]}
}
echo -n "The first day is 01-$1-$2,-- "
fday="$1-$2-01"
echo -n "$fday"
jd2dow $(date2jd $1 $2 01)
echo -n "The last day is $(lastday $1 $2)-$1-$2,-- "
lday="$1-$2-$(lastday $1 $2)"
echo -n "$lday"
jd2dow $(date2jd $1 $2 $(lastday $1 $2))
# last day for month
if [ ! -n "$1" ]; then
echo "please pass parameters."
echo "script.sh YYYY MM"
exit 0
fi
lastday() {
# ja fe ma ap ma jn jl ag se oc no de
mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31')
year=$1
month=$2
if [ $month -ne 2 ] ; then
echo ${mlength[$month]}
return 0
fi
leap=0
((!(year%100))) && { ((!(year%400))) && leap=1 ; } || { ((!(year%4))) && leap=1 ; }
feblength=28
((leap)) && feblength=29
echo $feblength
}
# date to Julian date
date2jd() {
year=$1
month=$2
day=$3
lday=$(lastday $year $month) || exit $?
if ((day<1 || day> lday)) ; then
echo day out of range
exit 1
fi
echo $(( jd = day - 32075
+ 1461 * (year + 4800 - (14 - month)/12)/4
+ 367 * (month - 2 + (14 - month)/12*12)/12
- 3 * ((year + 4900 - (14 - month)/12)/100)/4
- 2400001 ))
}
jd2dow()
{
days=('Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday')
jd=$1
if ((jd<1 || jd>782028)) ; then
echo julian day out of range
return 1
fi
((dow=(jd+3)%7))
echo ${days[dow]}
}
echo -n "The first day is 01-$1-$2,-- "
fday="$1-$2-01"
echo -n "$fday"
jd2dow $(date2jd $1 $2 01)
echo -n "The last day is $(lastday $1 $2)-$1-$2,-- "
lday="$1-$2-$(lastday $1 $2)"
echo -n "$lday"
jd2dow $(date2jd $1 $2 $(lastday $1 $2))
How to run:
./script 2021 12
No comments:
Post a Comment