Advanced Linux Shell Scripting

Advanced Linux Shell Scripting

ยท

3 min read

Task - 1

  • Create "N" number of directories using shell scripts & loops.

  • Example create day1 day2 day3.... day100 directories.

      #!/bin/bash
    
      dir_name=$1
      start=$2
      end=$3
    
      for ((i=start; i<=end; i++))
      do
      mkdir "$dir_name$i"
      done
    
  • Execute above shell script as ./directories.sh Day 1 100 this will create directories starting from Day1 upto the Day100

Task - 2

  • When the script is executed as ./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

      #!/bin/bash
    
      dir_name=$1
      start=$2
      end=$3
    
      for ((i=start; i<=end; i++))
      do
      mkdir "$dir_name$i"
      done
    

Task -3

  • Create a Script to backup all your work done till now

      #!/bin/bash
      user=`whoami`
      if [ $user = 'elvish' ]
      then
           tar -cf /Users/elvish/Desktop/Linux/backup/bkp.tar /Users/elvish/Desktop/Linux &>/dev/null
           if [ $? -eq 0 ]
           then
                echo Backup taken successfully...
           else
                echo Error in taking backup file
           fi
      else
           echo "You are not authorized to run this script $0"
      fi
    

Task - 4

Read About Cron and Crontab, to automate the backup Script

  • Cron jobs are jobs that are scheduled to run at specific intervals. You might have a command to perform something every hour, or every day, or every 2 weeks. Or on weekends. They are very powerful, especially on servers to perform maintenance and automation.

  • The crontab command is the entry point to work with cron jobs.

  • The first thing you can do is to explore which cron jobs are defined by you:

  • crontab fields

    | Domain | Possible Ideal | Syntax | Explanation | | --- | --- | --- | --- | | {1.} Minute | 0 - 59 | 7**** | The Cron work is started whenever the fixed frequency displays 7 in the minute location. | | {2.} Hour | 0 - 23 | 07*** | The Cron task is triggered whenever the control signal reads 7 a.m. (7 pm would be encoded as 19). | | {3.} Day | 0 - 31 | 007** | The day of the period is 7, so the Cron task operates every seventh day of the season. | | {4.} Month | 0 - Null and 10 - December | 0007* | The mathematical period is 7, indicating that the task is only available in July month. | | {5.} Day of the week | 0 - Sunday and 7 - Sunday | 00**7 | The number 7 in the present location indicates that the task would only be available on Sundays. |

  • Run crontab -e to edit the cronjob and add new cronjob

    The Above cronjob will run at

    • 2023-03-26 00:59:00 UTC

    • 2023-03-27 00:59:00 UTC

    • 2023-03-28 00:59:00 UTC..... and so on

Task - 5

User management:

  • A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations.

  • Each user is assigned an ID that is unique for each user in the operating system.

Add User:

Assign Password to user:

View added user:


Happy Learning ๐Ÿ˜Š

Thank yoy for the reading,

Hope you found this article useful.

https://www.linkedin.com/in/vishal-ranmale-907307114/

ย