Shell Scripting- Part2

Shell Scripting- Part2

·

2 min read

  • Write an Example of If else in Shell Scripting that takes2 inputs from user and compares them

      #!/bin/bash
    
      echo "Enter the first number"
      read firstnumber
    
      echo "Enter the second number"
      read secondnumber
    
      if [ $firstnumber -gt $secondnumber ]
      then
       echo "$firstnumber is the greatest"
      else
       echo "$secondnumber is the greatest"
      fi
    

  • Write a Shell Script to take user input(enter the day), input from arguments(enter your name) and print the variable

      #!/bin/bash
         echo "Hi there! Can you please tell me what day is it"
         read day
    
         echo "Hey $1, $day is a good day to start that thing which you have always been postponing :)"
    

  • Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is the directory name and the second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

      #!/bin/bash
    
      cd /home/ubuntu/devops/batch3/shell/assignments
    
      directory=$1
      start_file=$2
      end_file=$3
    
      for ((i=start_file; i<=end_file; i++))
      do
       mkdir $1$i
      done
    

  • Automating the above script to run at 1 am every day using cron tab

      0 1 * * * . /home/ubuntu/devops/batch3/shell/assignments/backup.sh
    
  • Create 2 users and just display their Usernames

  •   ubuntu@ip-172-31-19-16:~$ sudo useradd user-1
      ubuntu@ip-172-31-19-16:~$ sudo useradd user-2
      ubuntu@ip-172-31-19-16:~$ sudo cat /etc/passwd | tail -2