Wednesday, December 21, 2022

Find the number of repeatition of each word in a file using bash script without using array

#!/usr/bin/bash
echo "Please enter a sentence"
read sentence
for word in $(echo $sentence|xargs -n 1 |sort -u )

do
#occurance=$(echo $sentence | grep -i -o "$word"  | wc -w)

echo -e "$word-$(echo $sentence | grep -i -o "$word"  | wc -w)"



done

Thursday, December 15, 2022

Find highest and lowest number in an unsorted array using bash script

 #!/usr/bin/bash
#declare -a sports=([0]=football,[1]=cricket,[2]=hockey,[3]=basketball)
#sports[0]=foorball
#sports[1]=cricket
#sports[2]=hockey
#sports[3]=basketball

#echo "${sports[@]}"
echo "Please enter 5 marks of 5 students"
#for i in $(seq 0 4)
for i in {0..4}
do
read numbers[$i]
done
((max=${numbers[0]}))
((min=${numbers[0]}))
#echo "max is $max"
#echo "min is $min"
#echo "${numbers[@]}"
for i in ${numbers[@]}
do
if [ $i -gt $max ]
then
max=$(($i))
fi
if [ $i -lt $min ]
then
min=$(($i))
fi
done

echo "The highest number in the array is $max"
echo "The smallest number in the array is $min"

Find the number of repeatition of each word in a file using bash script

 #!/bin/bash
#input="./maharshi.txt"
#xyz=$1
#while read -r line
#do
#echo -e "$line"
#done <$xyz
declare -A words
file=$(cat ./maharshi.txt)
for line in $file
do

#echo -e "$line"
for word in $line
do
((words[$word]++))
done
done

for i in "${!words[@]}"
do
echo "$i " "${words[$i]}"
done



Monday, December 12, 2022

finding factorial of a number using bash one liner

 read -rep $'enter the number whose factorial you want\n' fact && result=1 && for i in $(seq 1 $fact) ;do result=$((result*i));don
e && echo $result

 

 

read -rep $'enter number\n' num && (echo 1; seq $num) | paste -s -d "\*" | bc

Performing x to the power of y in bash

sourav@LAPTOP-HDM6QEG8:~$ a=2
sourav@LAPTOP-HDM6QEG8:~$ b=8
sourav@LAPTOP-HDM6QEG8:~$ echo "$((a**b))"

Write a shell script to print all the multiplication tables (up to 10) between two given numbers

#!/bin/bash
echo "Enter first number"
read first
echo "Enter Second number"
read second

for ((i=first;i<=second;i++))
do
for((j=1;j<=10;j++))
do
echo  "$i * $j =  $((i*j))"
done

done

 

 using an one liner

 

read -rep $'Enter value of first\n' first && read -rep $'Enter value of second\n' second && for i in $(seq $first $second); do for j in $(seq 1 10); do echo "$i*$j=$((i*j))"; done;done

Wednesday, December 7, 2022

An one liner to print a table for the number given as input in bash

read -rep $'Enter value of j\n' j && for i in $(seq 10);do echo "$i * $j = "$(($i*$j)); done


otherwise the program would be something like


#!/bin/bash
clear
echo "input number :"
read x
echo
for i in 1 2 3 4 5 6 7 8 9 10
do
#t=`expr $x \* $i`
#echo $t
echo $(( x * i))
i=`expr $i + 1`
done

Tuesday, December 6, 2022

Calculating factorial using bash one liner

 

I was trying to get the factorial of a number in bash,it's an easy program obviously ,using loop I can do this,for example

#!/usr/bin/bash
echo "Enter number"
read number

result=1
for((i=1;i<=number;i++))
        do
                result=$(($result*i))
        done
echo "The factorial of $number is $result"

Then I tried to find some one liners,I mean bash is famous for one liners ,using seq and bc it worked just fine.

sourav@LAPTOP-HDM6QEG8:~$ num=5
sourav@LAPTOP-HDM6QEG8:~$ seq -s "*" 1 $num | bc
120

Using $(( it also worked like it should

sourav@LAPTOP-HDM6QEG8:~$ echo $((`seq -s "*" 1 $num`))
120

However when I am trying to use expr I am not able to do it.

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " * " 10`
expr: syntax error: unexpected argument ‘0’

I thought since * is an universal symbol may be I should escape it,but it still does not work

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " \* " 10`
expr: syntax error: unexpected argument ‘\\*’

However I am able to perform the summation though like this

sourav@LAPTOP-HDM6QEG8:~$ expr `seq -s " + " 10`
55

So why I am getting an error when trying to get the multiplication of a series using expr,can someone explain please??

Friday, December 2, 2022

Calculate total size of all the text files in current directory in bash

 du -bc *.txt | tail -1 | cut -f 1

 if the extension is variable

ext="txt"

du -bc *.$ect | tail -1 | cut -f 1


Source: https://unix.stackexchange.com/questions/72661/show-sum-of-file-sizes-in-directory-listing

Concatenate strings in bash shell script

 #!/bin/bash

echo "Enter first string"

read string1

echo "Enter second string"

read string2

echo "$string1 $string2"


Basic calculator in bash script



echo "Enter operation to be performed so that first number operation second number"

read operation

case $operation in

+)

result=`echo "scale=2; $first + $second" | bc`

;;

-)

result=`echo "scale=2; $first - $second" | bc`


;;

\*)

result=`echo "scale=2; $first * $second" | bc`


;;

/)

result=`echo "scale=2; $first / $second" | bc`


;;

*)

result="Not valid"

;;

esac


echo "Result is $result"


switch case example in bash script

#! /bin/bash


echo -en "Enter your logins\nUsername: "

read user_name 

echo -en "Password: "

read user_pass 

while [ -n $user_name -a -n $user_pass ]

do


case $user_name in

    ro*|admin)

        if [ "$user_pass" = "Root" ];

        then

            echo -e "Authentication succeeded \ n You Own this Machine"

    break

        else

            echo -e "Authentication failure"

            exit

        fi

    ;;

    jenk*)

if [ "$user_pass" = "Jenkins" ];

then

echo "Your home directory is /var/lib/jenkins"

    break

else

        echo -e "Authentication failure"

fi

        break

    ;;

    *)

        echo -e "An unexpected error has occurred."

        exit

    ;;

esac


done


Source: BASH "switch case" in Linux with practical example - Linux Cent

why echo --help not opening the help page of echo in bash

 man echo relates to the echo program. GNU echo supports a --help option, as do some others. When you run echo in Bash you instead get its builtin echo which doesn't.

To access the echo program, rather than the builtin, you can either give a path to it:

/bin/echo --help

or use Bash's enable command to disable the built-in version:

$ enable -n echo
$ echo --help

Bash has built-in versions of a lot of basic commands, because it's a little faster to do that, but you can always bypass them like this when you need to.


Source: https://unix.stackexchange.com/questions/153660/why-echo-help-doesnt-give-me-help-page-of-echo

Meaning of -n in bash

 


The -n argument to test (aka [) means "is not empty". The example you posted means "if $1 is not not empty. It's a roundabout way of saying [ -z "$1" ]; ($1 is empty).

You can learn more with help test.

$1 and others ($2$3..) are positional parameters. They're what was passed as arguments to the script or function you're in. For example, running a script named foo as ./foo bar baz would result in $1 == bar$2 == baz


Source: parameters - What does -n mean in Bash? - Stack Overflow