Skip to main content

shell scripting and important linux questions and answers

 1.echo $0 what it means?
    It will show the name of the currently running process
Example:

prepare test.sh

#!/bin/bash
echo $0

Then run test.sh after giving permission to chmod 700 test.sh

root@ip-172-31-15-196:~# chmod 700 test.sh
root@ip-172-31-15-196:~# ./test.sh
./test.sh

2.How to create 100 empty files at a time?
touch file-{001..100}


3.What is hardlink and softlink and how it will be work and what is the difference between them?

Hard link: 
1.If we delete original file our data will be available
2.Inode number of both original file and hard link file both are same
3.Hard links are fast .

Soft link:
1.If we delete original file our data will not be available
2.inode of original file and soft link file are different
3.Soft links are slower.
4.soft links can be used for linking directories
5.soft links will work across file systems.
6.File permissions are different.

Creating sof link

creating a file1.txt
[root@ip-172-31-43-210 linux]# echo "ashwika tech" >> file1.txt
[root@ip-172-31-43-210 linux]# ls
file1.txt

creating a soft link 
[root@ip-172-31-43-210 linux]# ln -s file1.txt softlink.file
[root@ip-172-31-43-210 linux]# ls -ltra
total 12
dr-xr-x---. 9 root root 4096 Jan  2 06:43 ..
-rw-r--r--. 1 root root   13 Jan  2 06:44 file1.txt
lrwxrwxrwx. 1 root root    9 Jan  2 06:44 softlink.file -> file1.txt

To check inode use ls -lia

[root@ip-172-31-43-210 linux]# ls -lia
total 12
 862604 drwxr-xr-x. 2 root root   44 Jan  2 06:44 .
4194433 dr-xr-x---. 9 root root 4096 Jan  2 06:43 ..
 862605 -rw-r--r--. 1 root root   13 Jan  2 06:44 file1.txt
 862615 lrwxrwxrwx. 1 root root    9 Jan  2 06:44 softlink.file -> file1.txt

Then removing soft link file 

[root@ip-172-31-43-210 linux]# rm file1.txt
rm: remove regular file 'file1.txt'? y
[root@ip-172-31-43-210 linux]# ls
softlink.file
[root@ip-172-31-43-210 linux]# cat softlink.file
cat: softlink.file: No such file or directory

Hard link

[root@ip-172-31-43-210 linux1]# echo "ashwika tech" >> file1.txt
[root@ip-172-31-43-210 linux1]# ls
file1.txt
[root@ip-172-31-43-210 linux1]# ln file1.txt hardlink.file
[root@ip-172-31-43-210 linux1]# ls
file1.txt  hardlink.file
[root@ip-172-31-43-210 linux1]# ls -lia
total 16
25166082 drwxr-xr-x.  2 root root   44 Jan  2 07:03 .
 4194433 dr-xr-x---. 10 root root 4096 Jan  2 07:02 ..
25166083 -rw-r--r--.  2 root root   13 Jan  2 07:02 file1.txt
25166083 -rw-r--r--.  2 root root   13 Jan  2 07:02 hardlink.file

removing file.txt

[root@ip-172-31-43-210 linux1]# rm file1.txt
rm: remove regular file 'file1.txt'? y
[root@ip-172-31-43-210 linux1]# ls
hardlink.file
[root@ip-172-31-43-210 linux1]# cat hardlink.file
ashwika tech

Note:our hard link file has data it was not deleted 

4.How to pass variables for script?
[root@ip-172-31-43-210 ~]# cat variables.sh
#!/bin/bash

a=$1
b=$2
p=$(($a*$b))
echo "The product of $a and $b = $p"

[root@ip-172-31-43-210 ~]# sh variables.sh 5 6
The product of 5 and 6 = 30

5.$#    Stores the number of command-line arguments 
   $?    Stores the exit value of the last command
           executed.
  $0    Stores the first word of the entered command (the 
      name of the shell program).
  $*    Stores all the arguments  ($1 $2 ...).
  "$@"  Stores all the arguments that were entered
      on the command line, individually quoted ("$1" "$2" ...).

6.what is the difference between $* and $@?

[root@ip-172-31-43-210 ~]# cat variables3.sh
#! /bin/bash

## No quotes $*
echo "Printing \$* "
for i in $*
do
echo i is: $i
done

## No quotes $@
echo "Printing \$@ "
for i in $@
do
echo i is: $i
done

## Quoted "$*"
echo "Printing \"\$*\" "
for i in "$*"
do
echo i is: $i
done

## Quoted "$@"
echo "Printing \"\$@\" "
for i in "$@"
do
echo i is $i
done


Then after executing script
[root@ip-172-31-43-210 ~]# sh variables3.sh 1 2 3 4 5 "6 7" 8
Printing $*
i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
i is: 6
i is: 7
i is: 8
Printing $@
i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
i is: 6
i is: 7
i is: 8
Printing "$*"
i is: 1 2 3 4 5 6 7 8
Printing "$@"
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6 7
i is 8

7.How to check RAM?
By using "dmidecode" we will check RAM

[root@ip-172-31-43-210 ~]# dmidecode
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.7 present.
11 structures occupying 378 bytes.
Table at 0x000EB01F.

Handle 0x0000, DMI type 0, 24 bytes
BIOS Information
        Vendor: Xen
        Version: 4.11.amazon
        Release Date: 08/24/2006
        Address: 0xE8000
        Runtime Size: 96 kB
        ROM Size: 64 kB
        Characteristics:
                PCI is supported
                EDD is supported
                Targeted content distribution is supported
        BIOS Revision: 4.11


Handle 0x0401, DMI type 4, 35 bytes
Processor Information
        Socket Designation: CPU 1
        Type: Central Processor
        Family: Other
        Manufacturer: Intel
        ID: F2 06 03 00 FF FB 8B 17
        Version: Not Specified
        Voltage: Unknown
        External Clock: Unknown

8.How to check particular user data?
By using finger command we will check user data
 #finger venkatesh
Login: venkatesh                        Name:
Directory: /home/venkatesh              Shell: /bin/sh
Never logged in.
No mail.
No Plan.

9.To check directory wise disk usage which command we will use?

du
du -h
[root@ip-172-31-43-210 ~]# du -h
4.0K    ./.ssh
0       ./aws-001
0       ./aws-002
0       ./aws-003
0       ./aws-004
0       ./aws-005
4.0K    ./linux
4.0K    ./linux1
76K     .

10.To check the free disk space which command we will use?
df -h
[root@ip-172-31-43-210 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        375M     0  375M   0% /dev
tmpfs           404M     0  404M   0% /dev/shm
tmpfs           404M   11M  393M   3% /run
tmpfs           404M     0  404M   0% /sys/fs/cgroup
/dev/xvda2       10G  1.6G  8.5G  16% /
tmpfs            81M     0   81M   0% /run/user/1000

11. what is the use of sed command in linux?

To insert perticular data in perticular line we wll use sed command.

example : we will create file1.txt

[root@ip-172-31-43-210 sed]# cat file1.txt
1
1
2
3
4
5
6
7
8
9

Then we can insert ABCD in 3rd line by using sed command

[root@ip-172-31-43-210 sed]# sed -i '3i\ABCD' file1.txt
[root@ip-172-31-43-210 sed]# cat file1.txt
1
1
ABCD
2
3
4
5
6
7
8
9

12.By using who last and lslogins commands what we will understand?
[root@ip-172-31-43-210 ~]# who
ec2-user pts/0        2022-01-04 10:06 (157.47.76.122)
ec2-user pts/1        2022-01-04 11:10 (157.47.76.122)

[root@ip-172-31-43-210 ~]# last
ec2-user pts/2        157.47.76.122    Tue Jan  4 11:28   still logged in
ec2-user pts/1        157.47.76.122    Tue Jan  4 11:10   still logged in

[root@ip-172-31-43-210 ~]# lslogins
  UID USER             PROC PWD-LOCK PWD-DENY  LAST-LOGIN GECOS
    0 root               89        0        1             root
    1 bin                 0        0        1             bin
    2 daemon              0        0        1             daemon
    3 adm                 0        0        1             adm
    4 lp                  0        0        1             lp
    5 sync                0        0        1             sync
    6 shutdown            0        0        1 Jan02/15:35 shutdown
    7 halt                0        0        1             halt
    8 mail                0        0        1             mail
   11 operator            0        0        1             operator
   12 games               0        0        1             games
   14 ftp                 0        0        1             FTP User
   29 rpcuser             0        0        1             RPC Service User
   32 rpc                 1        0        1             Rpcbind Daemon
   59 tss                 0        0        1             Account used for TPM access
   74 sshd                0        0        1             Privilege-separated SSH
   81 dbus                1        0        1             System message bus

13.To get current date, time, username and current working directory write a script.

# cat date.sh
#/bin/bash
echo "Hello, $LOGNAME"
echo "Today’s date is `date`"
echo "Username is `who i am`"
echo "Current directory is `pwd`"
echo "to show disk space `df -h`"
echo "to check memory `free -m`"

14. How to check directory is existing or not by using script
[root@ip-172-31-43-210 ~]# cat script.sh
#!/bin/bash
if [ -d /root/linux1 ];
then
        echo "The Directory Exists"
else
        echo "The Directory is not present"
fi
-----------------------------------
[root@ip-172-31-43-210 ~]# sh script.sh
The Directory Exists

15. How to check file is existing or not by using script
[root@ip-172-31-43-210 ~]# cat script.sh
#!/bin/bash
if [ -e /root/variables.sh ];
then
        echo "The file Exists"
else
        echo "The file is not present"
fi
------------------------------------------
[root@ip-172-31-43-210 ~]# sh script.sh
The file Exists

16. How to find word and replace that word with new one by using sed command?
 [root@ip-172-31-43-210 ~]# cat script.sh
#!/bin/bash
if [ -e /root/variables.sh ];
then
        echo "The file Exists"
else
        echo "The file is not present"
fi

In above script.sh file i am going to change present in to past by using sed command

[root@ip-172-31-43-210 ~]# sed -i 's/present/past/g' script.sh
[root@ip-172-31-43-210 ~]# cat script.sh
#!/bin/bash
if [ -e /root/variables.sh ];
then
        echo "The file Exists"
else
        echo "The file is not past"
fi

17.How to print square of numbers from 1 to 5 by using awk command

awk 'BEGIN { for(i=1;i<=5;i++) {print "square of",i,"is",i*i;}}'

[root@ip-172-31-32-143 ~]# awk 'BEGIN { for(i=1;i<=5;i++) {print "square of",i,"is",i*i;}}'
square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25

18. By using awk command how we can print line number befor each line?
 awk '{print NR, $0}' test1.sh

[root@ip-172-31-32-143 ~]# awk '{print NR, $0}' test1.sh
1 sed -i 's/old-text/new-text/g' input.txt
2 The s is the substitute command of sed for find and replace
3 It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt
4 Verify that file has been updated:
5 more input.txt
6 Let us see syntax and usage in details.
7 ab

19. By using awk command how we can find how many number of lines in a file?

awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' filename

[root@ip-172-31-32-143 ~]# awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' test1.sh
215

20.How we can add numbers by using function
#! /bin/bash
function add()
{
echo  –n "enter number:"
read a
echo  –n  "enter 2nd number :"
read b
echo "addition  is: $(( a+b ))"
}
add

[root@ip-172-31-32-143 ~]# sh function.sh
–n enter number:
4
–n enter 2nd number :
5
addition  is: 9

21.How we can add multiple values?
#! /bin/bash
sum=0
for (( counter=1 ; counter<5 ;counter++))
do
echo –n "enter your number"
read n
(( sum+=n))
#echo –n "$counter"
done
printf   "\n"
echo "result is : $sum"

[root@ip-172-31-32-143 ~]# sh adding.sh
–n enter your number
5
–n enter your number
6
–n enter your number
6
–n enter your number
6

22. while example

#!/bin/sh

a=0

while [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

[root@ip-172-31-32-143 ~]# sh while.sh
0
1
2
3
4
5
6
7
8
9

23. Loop example

#!/bin/bash
#Start of for loop
for a in 1 2 3 4 5 6 7 8 9 10
do
        # if a is equal to 5 break the loop
        if [ $a == 10 ]
        then
                break
        fi
        # Print the value
        echo "Iteration no $a"
done

[root@ip-172-31-32-143 ~]# sh loop.sh
Iteration no 1
Iteration no 2
Iteration no 3
Iteration no 4
Iteration no 5
Iteration no 6
Iteration no 7
Iteration no 8
Iteration no 9

24.example on and
#!/bin/bash

read -p "Enter First Numeric Value: "  first
read -p "Enter Second Numeric Value: "  second

if [ $first -le 10 ]  && [ $second -gt 20 ]
then
    echo "Both conditions are true"
else
    echo "Atleast one conditions is false"
fi

Enter First Numeric Value: 2
Enter Second Numeric Value: 2
Atleast one conditions is false

25.example on compare

#!/bin/bash
X=10
Y=20
if [ $x –gt $y ]
then
echo " x is greater than y"
else
echo "y is greater than x"
fi

[root@ip-172-31-32-143 ~]# sh compare.sh
 x is greater than y

26.example on OR

#!/bin/bash

read -p "Enter First Numeric Value: " first
read -p "Enter Second Numeric Value: " second

if [ $first -le 10 ] || [ $second -gt 20 ]
then
    echo "Atleast one conditions is true"
else
    echo "Both conditions are failed"
fi

[root@ip-172-31-32-143 ~]# sh or.sh
Enter First Numeric Value: 6
Enter Second Numeric Value: 5
Atleast one conditions is true


27.How to use find command

find / -name test.txt

By this command we can search file in entire drive



















 

Comments

Popular posts from this blog

How to copy files from one server to another server by using ansible copy module

 We have two servers 1.master 2.worker1 create playbook example: play.yml ---   - hosts: all     tasks:     - name: Ansible copy file to remote server       copy:        src: ~/kube-cluster        dest: /root Run ansible playbook  ansible-playbook play.yml

Kubernetes interview questions and answers

1.H ow to setup kubernetes dashboard on ubuntu16.04 cluster? To create kubernetes dashboard follow below link https://docs.aws.amazon.com/eks/latest/userguide/dashboard-tutorial.html To deploy the Metrics Server kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml kubectl get deployment metrics-server -n kube-system Deploy the dashboard kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml root@ip-172-31-43-76:~# kubectl get svc -n kubernetes-dashboard NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)         AGE dashboard-metrics-scraper   ClusterIP   10.102.6.123   <none>        8000/TCP        120m kubernetes-dashboard        NodePort    10.99.44.152   <none>        443:32508/TCP   120m Here a seperate service number was allocated 32508 Finally you can check on browser with your master or worker public ip with portn