CODE
#!/bin/bash echo "File Name: $0" echo "First Parameter : $1" echo "Second Parameter : $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters : $#"
OUTPUT
File Name: script.sh First Parameter : apple Second Parameter : banana Quoted Values: apple banana Quoted Values: apple banana Total Number of Parameters : 2
CODE
#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
echo $1
OUTPUT (If passed 'one two')
one two one
CODE
#!/bin/bash echo "Enter first number" read x echo "Enter second number" read y sum=$(( $x + $y )) echo "The result of addition=$sum"
OUTPUT
Enter first number 10 Enter second number 20 The result of addition=30
CODE
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
OUTPUT
a is not equal to b
CODE
#!/bin/bash
echo "enter no"
read n
if [ $((n%2)) == 0 ]
then
echo "The number is even."
else
echo "The number is odd."
fi
OUTPUT
enter no 7 The number is odd.
CODE
#!/bin/bash
a=12
if [ `expr $a % 2` == 0 -a $a -gt 10 ]
then
echo "$a is even and greater than 10."
else
echo "$a failed the test."
fi
OUTPUT
12 is even and greater than 10.
CODE
#!/bin/bash
# filename: aa.sh
echo "enter your choice"
read mycase
case $mycase in
1) echo "You selected unix";;
2) echo "You selected php";;
3) echo "You selected java";;
4) echo "You selected c++";;
5) exit;;
esac
OUTPUT
enter your choice 3 You selected java
CODE
#!/bin/bash
echo "enter vowel"
read mycase
case $mycase in
'a') echo "vowel";;
'e') echo "vowel";;
'i') echo "vowel";;
'o') echo "vowel";;
'u') echo "vowel";;
*) echo "consonant" ;;
esac
OUTPUT
enter vowel e vowel
CODE
#!/bin/bash
echo -n "Are you a student? [yes or no]: "
read response
case $response in
"Y" | "y" | "YES" | "Yes" | "yes")
echo -n "Yes, I am a student."
;;
"N" | "n" | "No" | "NO" | "no" | "nO")
echo -n "No, I am not a student."
;;
*)
echo -n "Invalid input"
;;
esac
OUTPUT
Are you a student? [yes or no]: yes Yes, I am a student.
CODE
#!/bin/bash
echo "Enter your lucky number"
read n
case $n in
101) echo "You got 1st prize" ;;
510) echo "You got 2nd prize" ;;
999) echo "You got 3rd prize" ;;
*) echo "Sorry, try for the next time" ;;
esac
OUTPUT
Enter your lucky number 510 You got 2nd prize
CODE
#!/bin/bash
for i in {1..5}
do
sleep $i &
done
echo "Background processes started..."
wait
echo "All processes have completed!"
OUTPUT
Background processes started... (Pauses for 5 seconds) All processes have completed!
CODE
#!/bin/sh
for FILE in $HOME/.bash*
do
echo $FILE
done
OUTPUT
/home/user/.bash_history /home/user/.bash_logout /home/user/.bashrc
CODE
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
OUTPUT
0 1 2 3 ... 9
CODE
#!/bin/bash
for (( i=1 ; i<=5 ; i++ ))
do
for (( j=1 ; j<=i ; j++ ))
do
echo -n "*"
done
echo " "
done
OUTPUT
* ** *** **** *****
CODE
#!/bin/bash
for (( i=1 ; i<=5 ; i++ ))
do
for (( j=1 ; j<=i ; j++ ))
do
echo -n "$i"
done
echo " "
done
OUTPUT
1 22 333 4444 55555
CODE
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
OUTPUT
Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times
CODE
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
echo "Welcome $i times"
done
OUTPUT
Bash version 5.1.16(1)-release... Welcome 0 times Welcome 2 times Welcome 4 times Welcome 6 times Welcome 8 times Welcome 10 times
CODE
#!/bin/bash
echo "Enter a number (N):"
read N
sum=0
for (( i=1; i<=$N; i++ )); do
sum=$((sum + i))
done
echo "Sum of integers from 1 to $N is: $sum"
OUTPUT
Enter a number (N): 5 Sum of integers from 1 to 5 is: 15
CODE
#!/bin/bash echo "Enter the word to search for:" read target_word echo "Enter the filename:" read filename count=$(grep -o -w "$target_word" "$filename" | wc -l) echo "The word '$target_word' appears $count times in '$filename'."
OUTPUT
Enter the word to search for: bash Enter the filename: script.sh The word 'bash' appears 2 times in 'script.sh'.
CODE
#!/bin/bash input_file="in.txt" output_file="output.txt" sort "$input_file" | uniq > "$output_file" echo "Duplicate lines removed successfully."
OUTPUT
Duplicate lines removed successfully.
CODE
#!/bin/bash
generate_password()
{
tr -dc 'A-Za-z0-9!@#$%^&*()_+{}[]' < /dev/urandom | fold -w 12 | head -n 1
}
password=$(generate_password)
echo "Generated password: $password"
OUTPUT
Generated password: aB9!zQx@1Lm#
CODE
#!/bin/bash
directory="$1"
if [ -z "$directory" ]; then
echo "Usage: $directory"
exit 1
fi
if [ ! -d "$directory" ]; then
echo "Error: '$directory' is not a valid directory."
exit 1
fi
echo "Empty files in $directory:"
find "$directory" -type f -empty
OUTPUT (If passed './logs')
Empty files in ./logs: ./logs/debug.log ./logs/trace.log
CODE
#!/bin/bash
host="$1"
if [ -z "$host" ]; then
echo "Usage: $host"
exit 1
fi
ping -c 4 "$host"
if [ $? -eq 0 ]; then
echo "$host is reachable."
else
echo "$host is not reachable."
fi
OUTPUT (If passed 'google.com')
PING google.com (142.250.190.46) 56(84) bytes of data. ... google.com is reachable.
CODE
#!/bin/bash
a=0
until [ $a -gt 10 ]
do
echo $a
a=`expr $a + 1`
done
OUTPUT
0 1 2 ... 10
CODE
#!/bin/bash
echo "Enter a Number"
read n
i=1
while [ $i -le 10 ]
do
echo " $n x $i = $(( n * i ))"
i=$(( i + 1 ))
done
OUTPUT
Enter a Number 5 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50
CODE
#!/bin/bash
rows=5
for((i=rows; i>=1; i--))
do
for((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
OUTPUT
* * * * * * * * * * * * * * *
CODE
#!/bin/bash clear p=$1 r=$2 n=$3 let int=`expr $p \* $r \* $n` int=`expr $int / 100` echo "simple interest , $int"
OUTPUT (If passed 1000 5 2)
simple interest , 100
CODE
#!/bin/bash
echo "enter the year"
read a
year=$a
a=$(( $a % 4 ))
if [ $a -eq 0 ];
then
echo "$year is leap"
else
echo "$year is not a leap year"
fi
OUTPUT
enter the year 2024 2024 is leap
CODE
#!/bin/bash
echo "Enter any number"
read n
if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi
OUTPUT
Enter any number 15 You won the game
CODE
#!/bin/bash
echo "Enter your lucky number"
read n
if [ $n -eq 101 ]; then
echo "You got 1st prize"
elif [ $n -eq 510 ]; then
echo "You got 2nd prize"
elif [ $n -eq 999 ]; then
echo "You got 3rd prize"
else
echo "Sorry, try for the next time"
fi
OUTPUT
Enter your lucky number 999 You got 3rd prize
CODE
#!/bin/bash
echo "enter file name"
read fname
if [ -f $fname ]; then
echo "$fname file exist"
else
echo "sorry, $fname file does not exist"
fi
OUTPUT
enter file name test.txt test.txt file exist
CODE
#!/bin/bash
function F1()
{
echo 'I like bash programming'
}
F1
OUTPUT
I like bash programming
CODE
#!/bin/bash
echo "Enter directory name"
read ndir
if [ -d "$ndir" ]
then
echo "Directory exist"
else
mkdir $ndir
echo "Directory created"
fi
OUTPUT
Enter directory name my_new_folder Directory created
CODE
#!/bin/sh
Hello ()
{
echo "Hello World $1 $2"
}
Hello Zara Ali
OUTPUT
Hello World Zara Ali
CODE
#!/bin/sh
Hello () {
echo "Hello World $1 $2"
return 10
}
Hello Zara Ali
ret=$?
echo "Return value is $ret"
OUTPUT
Hello World Zara Ali Return value is 10
CODE
#!/bin/sh
number_one ()
{
echo "This is the first function speaking..."
number_two
}
number_two ()
{
echo "This is now the second function speaking..."
}
number_one
OUTPUT
This is the first function speaking... This is now the second function speaking...
CODE
#!/bin/bash
echo -n "Enter Left-End: "
read le
echo -n "Enter Right-End: "
read ri
is_prime()
{
if [ $1 -lt 2 ]; then
return
fi
ctr=0
for((i=2;i<$1;i++))
{
if [ $(( $1 % i )) -eq 0 ]; then
ctr=$(( ctr +1 ))
fi
}
if [ $ctr -eq 0 ]; then
printf "%d " "$1"
fi
}
printf "Prime Numbers between %d and %d are: " "$le" "$ri"
for((i=le;i<=ri;i++))
{
is_prime $i
}
printf "\n"
OUTPUT
Enter Left-End: 10 Enter Right-End: 20 Prime Numbers between 10 and 20 are: 11 13 17 19
CODE
#!/bin/bash
is_odd()
{
x=$1
if [ $((x%2)) == 0 ]; then
echo "Invalid Input (Even)"
exit 1
else
echo "Number is Odd"
fi
}
is_odd 64
OUTPUT
Invalid Input (Even)
CODE
#!/bin/bash
add()
{
return $(($1+$2))
}
multiply()
{
return $(($1*$2))
}
add 3 4
multiply 3 4
add 5 4
ans=$?
echo "$ans"
OUTPUT
9
CODE
#!/bin/bash
math()
{
local a=$1
local b=$2
local sum=$(( a + b))
return $sum
}
math 5 10
echo "5 + 10 = $?"
OUTPUT
5 + 10 = 15
CODE
#!/bin/bash
function check_empty()
{
if [ -z "$1" ]; then
echo "Error: Argument is empty!"
return 1
fi
echo "Argument: $1"
}
check_empty "apple"
check_empty ""
OUTPUT
Argument: apple Error: Argument is empty!
CODE
#!/bin/bash
find_avg()
{
len=$#
sum=0
for x in "$@"
do
sum=$((sum + x))
done
avg=$((sum/len))
return $avg
}
find_avg 30 40 50 60
printf "%f" "$?"
printf "\n"
OUTPUT
45.000000
CODE
#!/bin/bash
function digital_clock
{
clear
while [ 1 ]
do
date +'%T'
sleep 1
clear
done
}
digital_clock
OUTPUT
14:30:15 (Will keep updating every second on terminal)
CODE
#!/bin/bash
compare()
{
file1=$1
file2=$2
cmp $file1 $file2
if [ $? -eq 0 ]
then
echo "Files are identical"
else
echo "Files are different"
fi
}
compare "file1.txt" "file2.txt"
OUTPUT (If files match)
Files are identical
SHORTCUTS
Command Check: which nano Undo: mu -> alt + u Clear: alt + t Copy: ctrl + shift + c Paste: ctrl + shift + v
INFO
(These are keyboard shortcuts to be executed directly in the terminal interface/nano text editor).
No comments:
Post a Comment