Showing posts with label linux teacher sourav. Show all posts
Showing posts with label linux teacher sourav. Show all posts

Saturday, October 19, 2024

Install java (openjdk and oraclejdk) on fedora 40



search openjdk version

dnf search openjdk

sudo dnf install java-latest-openjdk.x86_64




java -version




showing i installed version 21




now go the oracle jdk download page




https://www.oracle.com/java/technologies/downloads/?er=221886




I downloaded jdk-23_linux-x64_bin.tar.gz




sudo mkdir -p /usr/local/java




cd Downloads



sudo cp -r jdk-23_linux-x64_bin.tar.gz /usr/local/java



sudo tar zxvf jdk-23_linux-x64_bin.tar.gz




sudo nano /etc/profile



add the following lines





JAVA_HOME=/usr/local/java/jdk-23.0.1
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH


save the file and close
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk-23.0.1/bin/java" 1
echo $?
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk-23.0.1/bin/javac" 1
echo $?
sudo update-alternatives --install "/usr/bin/javaws.itweb" "javaws.itweb" "/usr/local/java/jdk-23.0.1/bin/javaws.itweb" 1
echo $?
sudo update-alternatives --set java /usr/local/java/jdk-23.0.1/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk-23.0.1/bin/javac
sudo update-alternatives --set javaws.itweb /usr/local/java/jdk-23.0.1/bin/javaws.itweb
source /etc/profile
reboot

now both java and javac command is working..

 

Source:https://phoenixnap.com/kb/fedora-install-java




































Friday, April 7, 2023

Some common usages of cat command

1) To view a single file 
Command: 
 

$cat filename

Output 
 

It will show content of given filename

 

2) To view multiple files 
Command: 

 

 

$cat file1 file2

Output 
 

This will show the content of file1 and file2.

 

 

3) To view contents of a file preceding with line numbers. 
Command: 
 

$cat -n filename

Output 
 

It will show content with line number
example:-cat -n  geeks.txt
 
1)This is geeks
2)A unique array

 

4) Create a file 
Command: 
 

$ cat > newfile

Output 
 

Will create a file named newfile

 

5) Copy the contents of one file to another file. 
Command: 
 

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

Output 
 

The content will be copied in destination file

 

6) Cat command can append the contents of one file to the end of another file. 
Command: 
 

$cat file1 >> file2

Output 
 

Will append the contents of one file to the end of another file
 

7) Cat command can display content in reverse order using tac command. 
Command: 
 

 $tac filename

Output 
 

Will display content in reverse order 
 

8) Cat command to merge the contents of multiple files. 
Command: 
 

$cat "filename1" "filename2" "filename3" > "merged_filename"

Output 
 

Will merge the contents of file in respective order and will insert that content in "merged_filename".
 
 
 

9) Cat command to display the content of all text files in the folder. 
Command: 
 

$cat *.txt

Output 
 

Will show the content of all text files present in the folder.
 

 

10) Cat command can suppress repeated empty lines in output 
Command: 
 

$cat -s geeks.txt

Output 

Will suppress repeated empty lines in output
 
 
Source:https://www.geeksforgeeks.org/cat-command-in-linux-with-examples/ 

 

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



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

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"


Wednesday, November 30, 2022

Make a duplicate copy of a specified file using bash shell script

 #!/usr/bin/bash

#if [ $# -lt 2 ] || [ $#  -gt 2 ]

#if test $# -lt 2 -o  $#  -gt 2

#if [[ $# -lt 2 ]] || [[  $#  -gt 2 ]]

if  (($# < 2)) || ((  $#  > 2 ))


then

echo invalid

exit

else

echo file to be copied : $1

echo new file name : $2

cp $1 $2

echo copy successful




fi

leap year program in bash shell script

#/usr/bin/bash

echo "Enter year"

read year

if [[ $(($year % 100)) -ne 0 ]]

#if (((year%100)!=0))

#if  [ $(($year % 100)) -ne 0 ]

then

if [ $(($year%4)) -eq 0 ]

then

echo "Leap Year"

else

echo "Not a leap year"

fi

else

if [ $(($year % 400)) -eq 0 ]

then

echo "Leap Year"

else

echo "Not a leap year"

fi

fi

Different ways to implement numerical comparisons using if else in bash

 #!/usr/bin/bash
echo "enter a number"
read num
if [[ $num -ge 1 ]] && [[ $num -lt 10 ]]
then
echo "A"
elif [[ $num -ge 10 ]] && [[ $num -lt 90 ]]
then
echo "B"
elif [[ $num -ge 90 ]] && [[ $num -lt 100 ]]
then
echo "C"
else
echo "D"
fi


#!/bin/bash
echo "enter a number"
read num
if (( num >= 1)) && ((num<10))
then
echo "sam"
elif ((num >= 10)) && ((num < 90 ))

then
echo "ram"
elif ((num >= 90)) && ((num  <100))
then
echo "rahim"
else
echo "tara"
fi

#!/bin/bash
echo "enter a number"
read num
if [ $num -ge 1 ] && [ $num -lt 10 ]
then
echo "A"
elif [ $num -ge 10 ] && [ $num -lt 90 ]
then
echo "B"
elif [ $num -ge 90 ] && [ $num -lt 100 ]
then
echo "C"
else
echo "D"
fi



Sunday, November 27, 2022

Get length of a string in bash shell script

#!/bin/bash

# Create a new string

mystring="lets count the length of this string"

i=${#mystring}

echo "Length: $i"


Source:https://www.hostinger.in/tutorials/bash-script-example

Thursday, November 24, 2022

Check if an username is present in /etc/passwd using shell script

 #!/bin/bash
read -rep $'Please enter username\n' username
#echo "you have entered $username"
if [ `grep -c $username /etc/passwd` -eq 0 ]
then
echo "$username is invalid"
else
echo "$username is valid"
fi

Tuesday, November 22, 2022

Find size of individual files of a particular extension in bash

 echo "enter extension"
read ext
for i in `find . -name "*.$ext" -type f`; do
   # echo "$i"
   echo `du -k "$i" | cut -f1`
done

Find files of a particular extension in bash

 echo "enter extension"
read ext
for i in `find . -name "*.$ext" -type f`; do
    echo "$i"
done

Find total size of files of an extension given as input in bash

 #!/bin/bash
echo "Please enter extension"
read ext
total=0
for i in $(pwd)/*.$ext

do

#filesize=$((`stat --printf="%s" $i`))
#filesize=$((`wc -c $i | awk '{print $1}'))
filesize=$((`wc -c $i | cut -d " " -f 1`))
total=$(($total+$filesize))
done

echo "The total size of $ext files in present directory is $total"

#alternate way


#!/usr/bin/bash
read -rep $'Please enter the extension\n' ext
total=0
for i in `ls`
do
if [[ "$i" == *"$ext"* ]]
then
#echo $i
total=$(($total + `du -k $i | cut -f 1`))
fi
done
echo "The total size of $ext files is $total"







Saturday, May 23, 2020

Array Example in Korn Shell

#!/bin/ksh -x
arr[0]="Hello"
arr[1]="World"
print "${arr[0]} ${arr[1]}"
#to print the array using index
index=1
print -n '${arr[index]}='
print  "${arr[index]}"
#to print the whole array
print "The whole array is ${arr[*]}"

Tuesday, April 21, 2020

Install powershell 7 on Debian 10

See the version of Debian

lsb_release -a

First I need to access all the commands stored in /usr/local/sbin and /usr/sbin

I need to edit the .bashrc file in my user's home profile and the last two lines at the end

export PATH=$PATH:/usr/local/sbin
export PATH=$PATH:/usr/sbin



save the file 


and 

Source .bashrc

to reload the file to see the change


to use the old ifconfig

sudo apt install net-tools



to see the dynamically generated ip 

ifconfig

I enabled ssh while installing so now using tool like putty I can access debian from outside

Now let me install cshell zshell and korshell

 sudo apt install csh zsh ksh

Now I am using Debian 10 which I can verify by using  lsb_release -a command

So to install powershell I have to use these commands



# Download the Microsoft repository GPG keys
wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products
sudo apt-get update

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh





Source:https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7



Sunday, April 19, 2020

Install and enable SSH on Lubuntu 19.10

sudo apt install net-tools

use ifconfig to see the automatically given ip

sudo apt install openssh-server

after installing to start stop and restart this service

sudo systemctl stop ssh

sudo systemctl start ssh

sudo systemctl restart ssh

To disable the SSH service to start during system boot run:

sudo systemctl disable ssh

To enable it again type:

sudo systemctl enable ssh

Ubuntu comes with a firewall configuration tool called UFW. If the firewall is enabled on your system, make sure to open the SSH port:

sudo ufw allow ssh





Source:https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-18-04/