0% found this document useful (0 votes)
52 views8 pages

Linux Terminal and Bash Scripting Guide

Uploaded by

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

Linux Terminal and Bash Scripting Guide

Uploaded by

ali allaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Cd In this exercise you will copy the commands from the terminal and paste it after the * sign.

############################### Section 1 #######################################


############################# Basic Commands ###################################

1- make a folder and name it nwp_training

$mkdir nwp_training

2- Enter the folder nwp_training

$cd nwp_training

3- make a text file and name it file_a

$touch file_a

4- copy file_a and name it file_b

$cp file_a file_b

5- show the contents of the folder nwp_training

$ls nwp_training

6- delete file_a

$rm file_a

7- show the contents again. How many files are there ? What are they ?

$ls
file_b

8- exit the folder nwp_training ( hint: go one folder up , where you fist started the exercise )

$cd ../

9 – copy the folder nwp_training and rename it nwp_training_2

$cp -r nwp_training nwp_training2

10 - copy file_b from folder nwp_training to nwp_training_2

$cp nwp_training/file_b nwp_training2

11 – copy [Link] file to nwp_training_2

$tar -xvzf linux_exercises.[Link]


$cp [Link] nwp_training2

12 – show the contents of the nwp_training_2


$ls nwp_training2

13 - show the contents of the [Link] file on terminal.

$nano [Link]
$cat [Link]

14 – show the line with the word “ SKINTEMP” from the [Link] file.

$grep SKINTEMP [Link]

16 – show the first 10 rows only from the [Link] file.

$head -10 [Link]

17 – show the first 20 rows only from the [Link] file.

$head -20 [Link]

18 - show the last 10 rows only from the [Link] file.

$tail -10 [Link]

19 - show the last 20 rows only from the [Link] file.

$tail -20 [Link]

20 - Download the zip file (mpich-3.0.4) using the terminal.


The link is
[Link]

$sudo apt install mpich


$wget (...the download link)

21- unzip the file using the terminal.

$ tar xf [Link]

22 – dump the data [Link] file into a file named metgrid_2.log

$cat [Link] > metgrid_2.log

23 – make folder script_test and enter it (cd)

$mkdir script_test
$cd script_test

24 – link [Link]

$ln -s [Link] script_test


25 - dump the first 100 lines from [Link] file into a file named metgrid_100.log

$head -100 [Link] > metgrid_100.log

26 - dump the last 10 lines from [Link] file into a file named metgrid_10.log

$tail -10 [Link] > metgrid_10.log

27 – add the 100 lines file on top of the 10 lines file

$head -100 metgrid_100.log / metgrid_10.log


$ metgrid_10.log >> metgrid_100.log

28 – delete the nwp_training folder

$rm -r nwp_training

29 – while in script_test folder, copy [Link] and [Link] from Desktop.

$cp ~/Desktop/[Link] ~/script_test

############################### Section 2 #####################################


############################# Bash Scripting ###################################

30 – Write a bash script that makes a folder named Data and then download and unzip the files
from step 20 & 21 in it. Name the bash script as [Link]
*********************
#!/bin/bash
mkdir data
cd data

wget
[Link]
[Link]

tar -xf [Link]


************************

31 – remove the folder Data then modify the script ,[Link] so it can print the phrase “ I have
finished the job” in the terminal.
********
#!/bin/bash
mkdir data
cd data

wget [Link]

tar -xf [Link]

rm -r ~/test/data
echo " I have finished the job"
31 - remove the folder Data then modify the script ,[Link] so it can print the phrase “ I have
finished the job at” then the put the date.
Hint. Use the command date to show the [Link]
*********
#!/bin/bash
mkdir data
cd data

wget [Link]

tar -xf [Link]

rm -r ~/test/data

echo " I have finished the job"


date
********

32 - remove the folder Data then modify the script ,[Link] so it save the phrase “ I have
finished the job at” then the put the date in a file name [Link].
Hint. Study question 22, 25, 26.
********
mkdir data
cd data

wget [Link]

tar -xf [Link]

rm -r ~/test/data

echo " I have finished the job"

date > ~/test/[Link]


********

33 – remove the folder Data then modify the script and define a variable Zip_File
and assign the variable to this value : [Link]
download the file by placing the Zip_File at the end of the weblink:
[Link]
then run the script
Hint. Be aware of the spacing when defining variable
*************
#!/bin/bash
mkdir data
cd data

Zip_file=[Link]
wget [Link]

tar -xf [Link]

rm -r ~/test/data

echo " I have finished the job"

touch ~/test/[Link]

date > ~/test/[Link]


**********

34 - Remove the command for removing the folder Data ( if there is a command in the script)
then modify the script and define a variable Zip_File and assign the variable to this value : zlib-
[Link]
then run the script.
*******
Zip_file=[Link]

#to download the file


wget [Link]

#to unzip the file


tar -xf [Link]
*******

############################### Section 3 #####################################


########################## Conditional Statements ##################################

35 – make a script named what_if.sh that define a variable named X, where X=10 .
• If X greater than or equal to 11 , then print “ the statement is false”

• If X less than 11, then print “ the statement is true”


**********
x=10
if [ x > 11 ] ; then
echo "the statment is true"
else
echo "the statment isn't true"
fi
**********

36 – modify the script so it ask the user to put a value of X.

37 - modify the script so :


• If X greater than 11 , then print “ the statement is false”
• If X equal to 11 , then print “ the statement is false”
• If X less than 11, then print “ the statement is true”
hint : use elif
*******
if (( x > 11 )) ; then
echo "the statment is true"
elif (( x == 11 )) ; then
echo "the statment is falseeeeeee"
else
echo "the statment is fals"
fi
********

38 - modify the script so :


• If X greater than 11 , AND less than 15 . then print “ the statement is true”
else , print the “statement is false”
• *****
• if (( 11 < x < 15 )) ; then
• echo "the statment is true"
• else
• echo "the statment is fals"
• fi
• *******

39 - modify the script so :


• If X greater than 11 , OR less than 15 . then print “ the statement is false”
else , print the “statement is true”
*****
• if (( x > 11 )) ; (( x < 15 )) ; then
• echo "the statment is fals"

• else
• echo "the statment is true"
• fi
• ********

############################### Section 4 #####################################


############################### Looping #####################################

40 – make a new script and name it [Link] ,


* make a variable N=1
* do a while loop so the script prints the values from 1 to 15 then it stops.

Hint. To add 1 to the N variable: N=$(( N+1 ))


******
N=1
echo $N
while (( $N < 15 ))
do
N=$((N+1))
echo $N
done
********
41 – modify the script from the previous step but using the until loop.
* What is the difference ?
*******
N=1
echo $N
until (( $N > 15 ))
do
N=$((N+1))
echo $N
done
*******
42 - modify the script to the previous step but using the for loop.
Hint . No need for N=$(( N+1 ))
******
N=1
for N in {1..15}
do
echo $N
done
******
43- modify the for script to print the even numbers from 2 to 60.
hint : add with an increment of 2.
********
N=1
for N in {2..60..2}
do
echo $N
done
******
43 - modify the script from the previous step but make the loop break for values greater than 10.
*********
N=1
for N in {2..60..2}
do
if (( $N > 10 )) ;
then break
fi
echo $N
done
********
44 - modify the script in step 42 but make the loop skip the value 10 .
hint . Use the continue command
******
N=1
for N in {2..60..2}
do
if (( $N == 10 )) ;
then
continue
fi
echo $N
done
*******

You might also like