The basics of Bash scripting for non-programmers. Part 3

In the second part of the article, we discussed script files, their parameters and access rights. We also talked about conditional execution, selection and loops. In this final part, we will take a look at the functions and scheduler of cron jobs. I will also provide various useful commands and links.





Functions

It makes sense to separate frequently used, repeating blocks into separate functions in order to run them if necessary by passing parameters.





The function definition looks like this:





<_>() {
  <>
  return <>
}

function <_>() {
  <>
  return <>
}
      
      



The first option is closer to the syntax of the C language and is considered more portable; in the second option, parentheses can be omitted. The return statement may also be missing if the function does not return a value.





The simplest example of a script containing a declaration and a function call would look like this:





#!/bin/bash

f() {
  echo Test
}

f
      
      



We declared a function f



that outputs the word Test, and then called it:





test@osboxes:~$ ./script6.sh
Test
      
      



Just like a script, a function can accept parameters and use them, referring to the number ($ 1, $ 2,…, $ N). The function call with parameters in the script is done like this:





< > <1> <2><N>
      
      



( ) 0 255. , 0, , . , $?



. , :





#!/bin/bash

summ() {
  re='^[0-9]+$'
  if ! [[ $1 =~ $re ]] ; then
    return 1
  elif ! [[ $2 =~ $re ]] ; then
    return 2
  else
    s=$(($1 + $2))
    return 0
  fi
}

summ $1 $2

case $? in
 0) echo "The sum is: $s" ;;
 1) echo "var1 is not a nubmer" ;;
 2) echo "var2 is not a nubmer" ;;
 *) echo "Unknown error" ;;
esac
      
      



summ, 2 ^[0-9]+$



, . , , 1, , 2. , s.





, , . , 0, , s



. :





test@osboxes.org:~$ ./script7.sh abc 123
var1 is not a nubmer
test@osboxes.org:~$ ./script7.sh 234 def
var2 is not a nubmer
test@osboxes.org:~$ ./script7.sh 10 15
The sum is: 25
      
      



, .. . , , , , . , "" , .





, , local, local loc_var=123



. , , , , .





, :





#!/bin/bash

clearFiles() {
  rm *.dat
  if [ $? -eq 0 ]
  then
    echo Files deleted
  fi
}

genFiles() {
  for (( i=1; i<=$1; i++ ))
  do
    head -c ${i}M </dev/urandom >myfile${i}mb.dat
  done
  ls -l *.dat
}

delFiles() {
for f in *.dat
  do
    size=$(( $(stat -c %s $f) /1024/1024 ))
    if [ $size -gt $1 ]
    then
      rm $f
      echo Deleted file $f
    fi
  done
  ls -l *.dat
}

showWeather() {
  curl -s "https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/$1" | grep "<desc" | sed -r 's/<description>//g; s/<\/description>//g'
}

menu() {
  clear
  echo 1 - Delete all .dat files
  echo 2 - Generate .dat files
  echo 3 - Delete big .dat files
  echo 4 - List all files
  echo 5 - Planet info
  echo 6 - Show weather
  echo "x/q - Exit"
  echo -n "Choose action: "
  read -n 1 key
  echo
}

while true
do
  case "$key" in
    "x" | "q" | "X" | "Q") break ;;
    "1")
      clearFiles
      read -n 1
    ;;
    "2")
      echo -n "File count: "
      read count
      genFiles $count
      read -n 1
    ;;
    "3")
      echo -n "Delete file greater than (mb): "
      read maxsize
      delFiles $maxsize
      read -n 1
    ;;
    "4")
      ls -la
      read -n 1
    ;;
    "5")
      ./script4.sh
      read -n 1
    ;;
    "6")
      echo -n "Enter city code: " # 524901 498817 5391959
      read citycode
      showWeather $citycode
      read -n 1
    ;;
  esac
  menu
done
      
      



5 :





  • clearFiles





  • genFiles





  • delFiles





  • showWeather





  • menu





while true, , menu . :





  • .dat

















  • ,









, , , , ( ), break . , , , , . :





test@osboxes.org:~$ ./script8.sh
1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 4
total 40
drwxr-xr-x 2 test test 4096 Feb 16 15:56 .
drwxr-xr-x 6 root root 4096 Feb 16 15:54 ..
-rw------- 1 test test   42 Feb 16 15:55 .bash_history
-rw-r--r-- 1 test test  220 Feb 16 15:54 .bash_logout
-rw-r--r-- 1 test test 3771 Feb 16 15:54 .bashrc
-rw-r--r-- 1 test test  807 Feb 16 15:54 .profile
-rw-r--r-- 1 test test 1654 Feb 16 12:40 input.xml
-rwxr-xr-x 1 test test  281 Feb 16 14:02 script4.sh
-rwxr-xr-x 1 test test  328 Feb 16 13:40 script7.sh
-rwxr-xr-x 1 test test 1410 Feb 16 15:24 script8.sh
      
      



1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 2
File count: 8
-rw-rw-r-- 1 test test 1048576 Feb 16 16:00 myfile1mb.dat
-rw-rw-r-- 1 test test 2097152 Feb 16 16:00 myfile2mb.dat
-rw-rw-r-- 1 test test 3145728 Feb 16 16:00 myfile3mb.dat
-rw-rw-r-- 1 test test 4194304 Feb 16 16:00 myfile4mb.dat
-rw-rw-r-- 1 test test 5242880 Feb 16 16:00 myfile5mb.dat
-rw-rw-r-- 1 test test 6291456 Feb 16 16:00 myfile6mb.dat
-rw-rw-r-- 1 test test 7340032 Feb 16 16:00 myfile7mb.dat
-rw-rw-r-- 1 test test 8388608 Feb 16 16:00 myfile8mb.dat
      
      



1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 3
Delete file greater than (mb): 5
Deleted file myfile6mb.dat
Deleted file myfile7mb.dat
Deleted file myfile8mb.dat
-rw-rw-r-- 1 test test 1048576 Feb 16 16:00 myfile1mb.dat
-rw-rw-r-- 1 test test 2097152 Feb 16 16:00 myfile2mb.dat
-rw-rw-r-- 1 test test 3145728 Feb 16 16:00 myfile3mb.dat
-rw-rw-r-- 1 test test 4194304 Feb 16 16:00 myfile4mb.dat
-rw-rw-r-- 1 test test 5242880 Feb 16 16:00 myfile5mb.dat
      
      



1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 1
Files deleted
      
      



1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 5
Enter the name of planet: Mars
The Mars has two satellite(s).
      
      



1 - Delete all .dat files
2 - Generate .dat files
3 - Delete big .dat files
4 - List all files
5 - Planet info
6 - Show weather
x/q - Exit
Choose action: 6
Enter city code: 524901
    Latest observations for Moscow from BBC Weather, including weather, temperature and wind information
      Temperature: -11°C (11°F), Wind Direction: Northerly, Wind Speed: 0mph, Humidity: 84%, Pressure: 1018mb, , Visibility: Moderate
      
      



: ( 6 ) curl, sudo apt install curl



.





cron

, , cron, .





crontab –l



. crontab –e



. cron :





m h dom mon dow command parameters
      
      



m – , h – , dom – , mon – , dow – , command – , parameters – . :





, , 10 30 , :





10,30 * * * 1-5 command parameter1 parameter2
      
      



, 15 :





*/15 * * * * command
      
      



, :





#!/bin/bash
USER=`whoami`
BACKUP_DIR=/tmp/backup_${USER}
BACKUP_FILE=${USER}_$(date +%Y%m%d%M%H%S).tgz

mkdir -p $BACKUP_DIR
cd /
tar -zcf $BACKUP_DIR/$BACKUP_FILE home/$USER
      
      



22:00, crontab -e



:





00 22 * * * ./backup_home.sh
      
      



, , crontab -l



:





test@osboxes.org:~$ crontab -l
00 22 * * * ./backup_home.sh
      
      



22:00 ( ):





test@osboxes.org:~$ cd /tmp/backup_test/
test@osboxes:/tmp/backup_test$ ll
total 80
drwxrwxr-x  2 test test 4096 Feb 16 16:38 ./
drwxrwxrwt 17 root root 4096 Feb 16 16:30 ../
-rw-rw-r--  1 test test 4431 Feb 16 16:30 test_20210216301601.tgz
-rw-rw-r--  1 test test 4431 Feb 16 16:31 test_20210216311601.tgz
-rw-rw-r--  1 test test 4431 Feb 16 16:32 test_20210216321601.tgz
-rw-rw-r--  1 test test 4431 Feb 16 16:33 test_20210216331601.tgz
-rw-rw-r--  1 test test 4431 Feb 16 16:34 test_20210216341601.tgz
test@osboxes:/tmp/backup_test$
      
      



, /tmp , .. , . .





: help

: <> --help

: man <>

: <> --version

: cat /etc/shells

: cat /etc/passwd

: pwd

: ls -la

: id

: set

: cat /etc/os-release

: uname -a

: sudo su -

Debian: apt install mc

(): top

: df -h

: du -ks /var/log

: ifconfig -a

: free -m

(): lsblk

: cat /proc/cpuinfo

: apt list --installed

: service --status-all

: service apache2 restart

: wget https://www.gnu.org/graphics/gplv3-with-text-136x68.png

- URL: curl https://www.google.com

: crontab -l

: crontab -e

: tail -f /var/log/syslog

: <> | wc -l

( ): chmod a+x <>

: ps -ef

, : ps -ef | grep <>

: cd -

( kill): kill -9

: rm < >

: rm -rf < >

: nano <_>

10 : ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -nr | head





bash: GNU Bash manual

Bash: Advanced Bash-Scripting Guide

: Bash

bash: SS64

Debian GNU/Linux: Debian FAQ





bash, , , . Bash , . .





That's all for now, I hope it was interesting!

What other useful commands do you know and use in your work?

What interesting designs did you have to use?

What tasks did you solve?

Happy scripting everyone, share your opinions in the comments!








All Articles