Shell Scripting

  • Uploaded by: SANJAY DUBEY
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Shell Scripting as PDF for free.

More details

  • Words: 2,025
  • Pages: 8
http://bash.cyberciti.biz/monitoring/find-null-password-accounts/

1)# Script to display sum of two number and to do calculations such as +, -, / etc # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------echo "**** My calculator ****" echo "M A I N - M E N U" echo "1. Multiplication" echo "2. Subtraction" echo "3. Remainder" echo "4. Divide" echo -n "Please select your choice (1-4) : " read choice echo read echo read

-n "Enter your first number : " n1 -n "Enter your second number : " n2

if [ $choice -eq 1 ] then answer="$n1 x $n2 = $(( $n1 * $n2 ))" elif [ $choice -eq 2 ] then answer="$n1 - $n2 = $(( $n1 - $n2 ))" elif [ $choice -eq 3 ] then Answer="$n1 % $n2 = $(( $n1 % $n2 ))" elif [ $choice -eq 4 ] then answer="$n1 / $n2 = $(( $n1 / $n2 ))" else echo "Sorry please select number between 1-4 only" exit 1 fi echo $answe 2)#!/bin/bash # Shell script to read 3 numbers and find the greaters of the three # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ----------------------------------------------------------------------

echo -n "Please enter three numbers (separate number by space) : " read a b c # compare a with b and c. Note -a is logical and operator if [ $a -gt $b -a $a -gt $c ] then big=$a elif [ $b -gt $a -a $b -gt $c ] # compare b with a and c then big=$b elif [ $c -gt $a -a $c -gt $b ] # compare c with a and b then big=$c elif [ $a -eq $b -a $a -eq $c -a $b -eq $c -a $c -eq $b ] # see if all of them are equal or not then big="All three numbers are same (equal)" else # something must be wrong if we are here, like one of number is character such as 'A' big="Can not guess greaters of three numbers" fi # display resul 3)#!/bin/bash # Shell script to read a number and write the number in words. Use case # control structure. For example 12 should be print as one two # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------echo -n "Enter number : " read n len=$(echo $n | wc -c) len=$(( $len - 1 )) echo "Your number $n in words : " for (( i=1; i<=$len; i++ )) do # get one digit at a time digit=$(echo $n | cut -c $i) # use case control structure to find digit equivalent in words case $digit in 0) echo -n "zero " ;; 1) echo -n "one " ;; 2) echo -n "two " ;; 3) echo -n "three " ;; 4) echo -n "four " ;; 5) echo -n "five " ;; 6) echo -n "six " ;; 7) echo -n "seven " ;; 8) echo -n "eight " ;;

9) echo -n "nine " ;;

esac done

# just print new line echo "" 4) !/bin/bash # Shell script to calulate area and Circumference of circle. # It take radius of a circle as input. # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------# Lesson on Circumference of a Circle : # Visit http://www.mathgoodies.com/lessons/vol2/circumference.html echo -n "Enter the radius of a circle : " read r # use formula to get it area=$(echo "scale=2;3.14 * ($r * $r)" | bc) # use formula to get it d=$(echo "scale=2;2 * $r"|bc) circumference=$(echo "scale=2;3.14 * $d"| bc) echo "Area of circle is $area" echo "Circumference of circle is $circumference" 5) #!/bin/bash # Script to read price of an article. If the price is less than 100 # then display "No discount" else give a discount of 10%. Display # the price of article after discount # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------echo -n "Enter price of an article : " read price if [ $price -lt 100 ] then echo "No discount " d=0 # 0 means no discount else echo "10% discount " d=$(( $price * 10 / 100 )) # 10% discount fi # how much user need to pay? after discount pay=$(( $price - $d )) echo "You need to pay INR. $pay" 6) #!/bin/bash

# # # # # # # # #

Shell program to calculate the number of digits in a number read from the user ----------------------------------------------Copyright (c) 2005 nixCraft project This script is licensed under GNU GPL version 2.0 or above ------------------------------------------------------------------------This script is part of nixCraft shell script collection (NSSC) Visit http://bash.cyberciti.biz/ for more information. -------------------------------------------------------------------------

echo -n "Enter number : " read n sd=0 # store number of digit nd=0 on=$n # store $n so that we can use it later # use while loop to caclulate the number of digit while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) nd=$(( $nd + 1)) # calculate all digit in a number till n is not zero done echo "Numnber of digit in a $on is $nd" 7) #!/bin/bash # Shell script to read a character (upper or lower), digit, special symbol and # display message according to the character entered # ----------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------char="" echo -n "Enter a one character : " read char # use sed command to determine input character is digit or not # -e : start script # s/[0-9]// : Attempt to match regex [0-9] i.e. all digit against the pattern space (echo $char). # If successful, replace that portion matched with replacement i.e. // do nothing. # g : Global replacement # so if digit are in input it will replace all those digit, and if input is only digit, nothing # is left i.e. string is zero and that is what tasted with -z if [ -z $(echo $char | sed -e 's/[0-9]//g') ] then echo "$char is Number/digit" elif [ -z $(echo $char | sed -e 's/[A-Z]//g') ] # find out if character is upper

then

echo "$char is UPPER character"

elif [ -z $(echo $char | sed -e 's/[a-z]//g') ] # find out if character is lower then echo "$char is lower character" else echo "$char is Special symbol" # else it is special charact 8) #!/bin/bash # Shell Script to ask user his/her name and then display it with a # GoodDay/Good morning message # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------echo "Hi! I'm $(hostname) computer!!" echo -n "May I know your name please ? " read yourname echo "Goo Day $yourname" 9) #!/bin/bash # Script to update user password in batch mode # You must be a root user to use this script # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------# /root is good place to store clear text password FILE="/root/batch.passwd" # get all non-root user account # By default on most linux non-root uid starts # from 1000 USERS=$(awk -F: '{ if ( $3 > 1000 ) print $1}' /etc/passwd) # create file with random password echo "Generating file, please wait..." # overwrite file, this is bash specific a better solution is cat > $FILE >$FILE for u in $USERS do p=$(pwgen -1 -n 8) # create random password echo "$u:$p" >> $FILE # save USERNAME:PASSWORD pair done echo "" echo "Random password and username list stored in $FILE file" echo "Review $FILE file, once satisfied execute command: " echo "chpasswd < $FILE"

# Uncomment following line if you want immediately update all users password, # be careful with this option, it is recommended that you review $FILE first # chpasswd < $FILE 10) #!/bin/bash # Shell script to Finding Accounts with No Password # Useful to improve system security # Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # For more info, please visit: # http://cyberciti.biz/shell_scripting/bmsinstall.php # TODO # - Disable all account w/o password # - Send an email to admin # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------SPATH="/usr/local/etc/bashmonscripts" INITBMS="$SPATH/defaults.conf" [ ! -f $INITBMS ] && exit 1 || . $INITBMS if ( isRootUser ); then $GREP -v -E "^#" $SHADOW_FILE | $AWK -F: '$2 == "" { print $1 }' else echo "Permission denied [$($ID -u)]" fi 11) #!/bin/bash # Script to display the text entered by the user in bold # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------# read man page of tput command. echo -n "Enter text : " read text echo "Here is what you entered " # use tput command to display it in bold tput bold echo "$text" echo -n "Press any key to continue..." # okay turn of bold effect read key # it will clear screen too tput reset 12) !/bin/bash # Shell program to display numbers from 1 to 10 # ----------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC)

# Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------# using for loop echo "Using for loop method # 1... " for i in 1 2 3 4 5 6 7 8 9 10 do echo -n "$i " done echo "" # this is much better as compare to above for loop echo "Using for loop method # 2... " for (( i=1; i<=10; i++ )) do echo -n "$i " done echo "" # use of while loop echo "Using while loop..." j=1 while [ $j -le 10 ] do echo -n "$j " j=$(( j + 1 )) # increase number by 1 done echo "" 13) #!/bin/bash # Shell script to read 5 digit number and calculate the sum of digit # ------------------------------------------------------------------------# Copyright (c) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------echo -n "Enter numnber : " read n # find out length of string using wc -c command len=$(echo $n | wc -c) # remove \n i.e. new line character len=$(( $len - 1 )) # use loop to go throug all digit one by one and calculate sum of digit on fly for (( i=1; i <= $len; i++ )) do sum=$(( $sum + $(echo $n | cut -c $i) )) done echo "Sum of $n is $sum"

Related Documents

Shell Scripting
April 2020 7
Shell Scripting
November 2019 24
Shell Scripting
November 2019 25
Shell-scripting
November 2019 27
Shell Scripting
June 2020 7
Linux Shell Scripting
April 2020 6

More Documents from ""