100% found this document useful (1 vote)
1K views8 pages

1) # Script To Display: Sum As

This document contains 9 scripts that perform various tasks related to calculations, data processing, and password management in Linux/Unix systems. The scripts allow a user to: 1) Perform basic math operations like addition, subtraction etc. and display the results 2) Find the greatest of three numbers entered 3) Convert a entered number to words (e.g. twelve for 12) 4) Calculate the area and circumference of a circle given the radius 5) Calculate discount on a product price and display final amount to be paid. 6) Count the digits in a number 7) Classify a character as alphabet, digit or symbol 8) Greet a user by name after asking for their name

Uploaded by

SANJAY DUBEY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
100% found this document useful (1 vote)
1K views8 pages

1) # Script To Display: Sum As

This document contains 9 scripts that perform various tasks related to calculations, data processing, and password management in Linux/Unix systems. The scripts allow a user to: 1) Perform basic math operations like addition, subtraction etc. and display the results 2) Find the greatest of three numbers entered 3) Convert a entered number to words (e.g. twelve for 12) 4) Calculate the area and circumference of a circle given the radius 5) Calculate discount on a product price and display final amount to be paid. 6) Count the digits in a number 7) Classify a character as alphabet, digit or symbol 8) Greet a user by name after asking for their name

Uploaded by

SANJAY DUBEY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 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 <http://cyberciti.biz/fb/>
# 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 -n "Enter your first number : "


read n1
echo -n "Enter your second number : "
read 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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 &lt; $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 <http:>
# 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 <http://cyberciti.biz/fb/>
# 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 <http://cyberciti.biz/fb/>
# 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"

You might also like