KEMBAR78
Linux Lab3 | PDF | Information Technology Management | Computing
0% found this document useful (0 votes)
111 views5 pages

Linux Lab3

The document provides instructions and examples for various scripting tasks in Linux/Bash including: - Creating a script to make directories and files in a specific numbered format - Storing command outputs in variables - Writing a script to check if a given path is a file, directory, or invalid - Examples of RPM package management commands - An assignment with 10 questions to write Bash scripts for various system querying and file/permission management tasks

Uploaded by

Saket Bande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views5 pages

Linux Lab3

The document provides instructions and examples for various scripting tasks in Linux/Bash including: - Creating a script to make directories and files in a specific numbered format - Storing command outputs in variables - Writing a script to check if a given path is a file, directory, or invalid - Examples of RPM package management commands - An assignment with 10 questions to write Bash scripts for various system querying and file/permission management tasks

Uploaded by

Saket Bande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

[Lab]

Create a script for the following task


Hint: [root@localhost scripts]# for i in {2..5};do touch file$i;done

Make 100 directories name test1 to 100


Each directory should have a file in the following manner:
• test1 should have file1
• test2 should have file2
.
.
.
• test100 should have file100

Each file should have the content in the following manner:


• file1 should have content 'This is file1'
• file2 should have content 'This is file2'
.
.
.
• file100 should have content 'This is file100'
-----
#!/bin/bash

#Comment: create multiple dir with files and content in it

#!/bin/bash

#Remove test dir if created already


rm -rf test*

#create 100 dir


mkdir test{1..100}

#defining loop for range 1-100


for i in {1..100}
do
cd test$i
echo "This is file$i" > file$i
cd ..

done
-----
How to put output of a command in variable
------------------------------------------
var=$(command)

[root@localhost scripts]# date


Sat May 29 13:06:32 IST 2021

Here we are redirecting the output of date command in to var


[root@localhost scripts]# var=$(date)
[root@localhost scripts]# echo $var
Sat May 29 13:06:57 IST 2021

Concept
--------
If the command o/p is null the variable will be blank or 0, else it will be non
zero

Script ques continued...


------------------------
[root@localhost scripts]# cat user-id-info.sh
#!/bin/bash

##This script will ask for username and print its username:userid
#concepts to learn in this script
#you will get to now how scripts take input from user
#you will get to know how to put command's o/p in a variable
#you will get to know the usage of if-else loop
#you will get to learn the logic of writing a script
#you will get to know the file for user info

read -p "Enter the username: " usr

myvar=$(cat /etc/passwd | awk -F':' '{print $1,$3}' | grep "$usr")

if [[ -n $myvar ]]
then
echo "$myvar"
else
echo "user not found"
fi

[ryan tutorials] - https://ryanstutorials.net/bash-scripting-tutorial/

Alias command
-------------
used to give canonical names to the commands
for ex: ll is also ls -l

alias cp='cp -i'


alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-
tilde'

-------
[root@localhost ~]# alias scr='ls /root/editiss/scripts'
[root@localhost ~]# scr
for.sh script1.sh user-id-info.sh
-------
[Aman G] 9516688917
useradd in Ubuntu - because it does not create home for that user.
-----------------------------------------------------------------

Create a script that asks for a path and tell if the entered path is a file or
directroy or doesnt exist.

[root@localhost scripts]# cat file_or_dir.sh


#!/bin/bash

#Take a path and check if it's a file or dir

read -p "Enter the path: " path

if [[ -f $path ]]
then
echo "$path --> file"
elif [[ -d $path ]]
then
echo "$path --> directory"
else
echo "$path --> invalid"

fi

--
[Vrushali] https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

[root@localhost scripts]# ./file_or_dir.sh


Enter the path: /etc/passwd
/etc/passwd --> file
[root@localhost scripts]# ./file_or_dir.sh
Enter the path: frog
frog --> invalid
--
RPM

RPM - Redhat package manager


Every utility in linux can be called as package (*.rpm)
Install the package
Once the package is installed, a service is created

Location of packages (*.rpm)


----------------------------
[root@localhost Packages]# pwd
/run/media/lavish/CentOS 7 x86_64/Packages
[root@localhost Packages]# find / -name 'Packages'
/run/media/lavish/CentOS 7 x86_64/Packages

Installing a package via rpm


----------------------------
rpm -ivh <package name>
i - install
v - verbose
h - hash marking

[root@localhost Packages]# rpm -ivh aide-0.15.1-13.el7.x86_64.rpm


warning: aide-0.15.1-13.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID
f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:aide-0.15.1-13.el7 ################################# [100%]

To find out if a package is installed or not on the system - we use command: rpm -
qa | grep <package name>
q - query
a - all
so , it queries amongst all the installed packages and finds your own package

[root@localhost Packages]# rpm -qa | grep aide


aide-0.15.1-13.el7.x86_64

What is AIDE - AIDE (Advanced Intrusion Detection Environment)

[Assignment Ques]

1. Write a script that performs following operations on the given set of files?

a) displays the total count of files


b) displays the count of *.pdf files and *.docx
c) rename all *.pdf to *.docx
d) displays the count of *.pdf files and *.docx
e) displays only those files which have underscore '_' in their names

msexchange.qlv
Necessary Compliance Violations.docx
New Doc 12-23-2020 16.51.pdf
Open Cases.csv
OPS-94632_new.tar
OS-Windows.pdf
QQL.json
Ransomware.docx
Red_Hat Reference Guide.pdf
Script Analysis.xlsx
Script page.jpg
SolarWinds_Supply-Chain_Attack_UDdashboard.json
Splunk_PC_App.png
Splunk_VM_App.png
Studio_Malware_July_2013.xlsx
Troubleshooting Agent Permission Issues for Windows.pdf
UDC_Demo_20210106.xml
Video 2020-11-10 at 7.12.43 PM.mp4
VMware-workstation-full-15.5.5-16285975.exe
VMware-workstation-full-16.0.0-16894299.exe
WhatsApp Image 2020-11-05 at 2.47.06 PM.jpeg
win.txt

2. Write a script that displays the user information on the linux box in the
following format:
Username Shell assigned to that user
3. Create ascript that asks for a username and displays it's group name
4. Create a script that asks for a file name and dislays its permission
5. Create a script that asks for a directory name and dislays its permission
6. Create a script that displays only those users whose UID > 1000
7. Create a script that asks for username and checks if that user is a sudoer
8. Create a script that asks for a processname and didplays it's PID
9. Create a script that:
- creates a dir 'test' with 100 files in it 'file{1..100}'
- provides executable permissions to others for file 1-50
- displays the name of all world executable files
- changes their permission to world-readable only
- displays the name of all world readable files woith permissions
10. Create a script that asks for a username and displays all the files owned by
that user.

You might also like