Exercise 1: Execute following requests using shell command
1. Move to your user directory
cd ~
2. Inside your user directory, create two subdirectories Slides, Labs and a file
name Solution.txt
mkdir Slides Labs
touch Solution.txt
3. Go to Labs directory
cd Labs
4. Inside Labs directory, create a subdirectory named Lab1 and a file named
Ex1.txt
mkdir Lab1
touch Ex1.txt
5. Copy file Ex1.txt from labs directory to Lab1 directory.
cp Ex1.txt ./Lab1
6. Remove file Ex1.txt inside Labs directory
rm Ex1.txt
7. Rename of file Ex1.txt to Exercise1.txt
cd ./Lab1
mv Ex1.txt Exercise1.txt
8. Add write mode access to file Exercise1.txt for other users
chmod o+w Exercise1.txt
9. Remove directory Slides
cd~
rmdir Slides
Exercise 2: Continue exercise 1, go to Lab1 directory and try this sequence of
commands and record results of these commands.
1. cd
2. pwd
/home/stevehoang
3. ls –al
total 108
drwxr-x--- 22 stevehoang stevehoang 4096 Feb 20 00:48 .
drwxr-xr-x 4 root root 4096 Feb 9 16:52 ..
-rw------- 1 stevehoang stevehoang 4609 Feb 23 23:27 .bash_history
-rw-r--r-- 1 stevehoang stevehoang 220 Mar 31 2024 .bash_logout
-rw-r--r-- 1 stevehoang stevehoang 3771 Mar 31 2024 .bashrc
drwx------ 21 stevehoang stevehoang 4096 Feb 17 18:20 .cache
drwx------ 20 stevehoang stevehoang 4096 Feb 19 23:33 .config
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:33 Desktop
drwxr-xr-x 3 stevehoang stevehoang 4096 Feb 11 09:01 Documents
drwxrwxr-x 3 stevehoang stevehoang 4096 Feb 11 09:08 .dotnet
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:32 Downloads
drwx------ 2 stevehoang stevehoang 4096 Feb 23 21:15 .gnupg
drwxrwxr-x 4 stevehoang stevehoang 4096 Feb 17 18:20 .java
drwxrwxr-x 3 stevehoang stevehoang 4096 Feb 20 00:47 Labs
drwx------ 4 stevehoang stevehoang 4096 Feb 9 16:52 .local
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Music
drwxr-xr-x 3 stevehoang stevehoang 4096 Feb 18 16:00 Pictures
drwx------ 3 stevehoang stevehoang 4096 Feb 9 16:58 .pki
-rw-r--r-- 1 stevehoang stevehoang 807 Mar 31 2024 .profile
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Public
drwx------ 10 stevehoang stevehoang 4096 Feb 20 23:49 snap
-rw-rw-r-- 1 stevehoang stevehoang 0 Feb 20 00:44 Solution.txt
drwx------ 2 stevehoang stevehoang 4096 Feb 19 23:35 .ssh
-rw-r--r-- 1 stevehoang stevehoang 0 Feb 9
16:56 .sudo_as_admin_successful
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:33 Templates
drwxrwxr-x 2 stevehoang stevehoang 4096 Feb 20 00:24 TestingFolder
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Videos
drwxrwxr-x 4 stevehoang stevehoang 4096 Feb 9 17:10 .vscode
4. cd .
5. pwd
/home/stevehoang
6. cd ..
7. pwd
/home
8. ls -al
total 28
drwxr-xr-x 4 root root 4096 Feb 9 16:52 .
drwxr-xr-x 23 root root 4096 Feb 9 16:49 ..
drwx------ 2 root root 16384 Feb 9 16:47 lost+found
drwxr-x--- 22 stevehoang stevehoang 4096 Feb 20 00:48 stevehoang
9. cd ..
10. pwd
11. ls -al
total 4194404
drwxr-xr-x 23 root root 4096 Feb 9 16:49 .
drwxr-xr-x 23 root root 4096 Feb 9 16:49 ..
lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Feb 26 2024 bin.usr-is-merged
drwxr-xr-x 4 root root 4096 Feb 9 17:04 boot
dr-xr-xr-x 2 root root 4096 Aug 27 23:22 cdrom
drwxr-xr-x 21 root root 5640 Feb 23 23:27 dev
drwxr-xr-x 139 root root 12288 Feb 19 23:33 etc
drwxr-xr-x 4 root root 4096 Feb 9 16:52 home
lrwxrwxrwx 1 root root 7 Apr 22 2024 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Apr 22 2024 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4096 Apr 8 2024 lib.usr-is-merged
drwx------ 2 root root 16384 Feb 9 16:47 lost+found
drwxr-xr-x 3 root root 4096 Feb 11 09:17 media
drwxr-xr-x 2 root root 4096 Aug 27 22:37 mnt
drwxr-xr-x 4 root root 4096 Feb 19 23:33 opt
dr-xr-xr-x 410 root root 0 Feb 23 23:27 proc
drwx------ 6 root root 4096 Feb 19 23:33 root
drwxr-xr-x 38 root root 940 Feb 23 23:34 run
lrwxrwxrwx 1 root root 8 Apr 22 2024 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Mar 31 2024 sbin.usr-is-merged
drwxr-xr-x 20 root root 4096 Feb 17 09:46 snap
drwxr-xr-x 2 root root 4096 Aug 27 22:37 srv
-rw------- 1 root root 4294967296 Feb 9 16:49 swap.img
dr-xr-xr-x 13 root root 0 Feb 23 23:27 sys
drwxrwxrwt 21 root root 4096 Feb 23 23:34 tmp
drwxr-xr-x 12 root root 4096 Aug 27 22:37 usr
drwxr-xr-x 14 root root 4096 Feb 9 16:51 var
Exercise 3: Create file Hello.txt inside Lab1 directory. Then using echo
command and redirecting to write below contents to file Hello.txt.
Hello everyone!
This is Advanced Programming Techniques course.
There are many exercises for you in this course.
Let’s practice what we learn in this course with exercises.
Are you ready?
echo "Hello everyone!" > ~/Lab1/Hello.txt
echo "This is Advanced Programming Techniques course." >>
~/Lab1/Hello.txt
echo "There are many exercises for you in this course." >> ~/Lab1/Hello.txt
echo "Let’s practice what we learn in this course with exercises." >>
~/Lab1/Hello.txt
echo "Are you ready?" >> ~/Lab1/Hello.txt
Exercise 4: Use the I/O redirection and pipe mechanisms and Unix commands
to perform the following tasks:
1. Count number of word course appeared in file Hello.txt (you created in
exercise 3) and save results to file Ex4_results.txt.
grep -o 'course' Hello.txt
2. Print 3rd line of the file Hello.txt, then count the number of words in this
line and save results to the end of file Ex4_results.txt. (Hint: using sed, wc
and pipe mechanism)
Command: sed -n '3p' example.txt | wc -w
3. Store the names and attributes of the directories and files inside your user
directory.
Command: ls -l ~ > file_attributes.txt
total 44
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:33 Desktop
drwxr-xr-x 3 stevehoang stevehoang 4096 Feb 11 09:01 Documents
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:32 Downloads
drwxrwxr-x 3 stevehoang stevehoang 4096 Feb 20 00:47 Labs
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Music
drwxr-xr-x 3 stevehoang stevehoang 4096 Feb 18 16:00 Pictures
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Public
drwx------ 9 stevehoang stevehoang 4096 Feb 17 18:20 snap
-rw-rw-r-- 1 stevehoang stevehoang 0 Feb 20 00:44 Solution.txt
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 19 23:33 Templates
drwxrwxr-x 2 stevehoang stevehoang 4096 Feb 20 00:24 TestingFolder
drwxr-xr-x 2 stevehoang stevehoang 4096 Feb 9 16:52 Videos
4. Count total the number of regular files and directories inside your user
directory and save it to the end of file Ex4_results.txt.
Command: echo "Regular files: $(find ~ -type f | wc -l)" >> Ex4_results.txt
Regular files: 43824
Command: echo "Directories: $(find ~ -type d | wc -l)" >> Ex4_results.txt
Directories: 3660
5. Count total the number of directories inside your user directory and save it
to the end of file Ex4_results.txt.
Command: echo "Directories: $(find ~ -type d | wc -l)" >> Ex4_results.txt
Directories: 3660
6. Count the total number of processes currently in the system and save
results to the end of file Ex4_results.txt
Command: echo "Total processes: $(ps aux | wc -l)" >> Ex4_results.txt
Total processes: 392
Exercise 5: Write a set of commands in a shell script file named greeting.sh
that does the following:
a) Contains a comment with your name, the name of this script, and the
purpose of this script.
b) Displays the string “Hello + username”
c) Displays the name and version of this operating system.
d) Displays the group ID (GID) of the current user.
e) Displays the current date and time.
h) Displays a list of all the files in the current user’s directory.
i) Displays the values of the TERM, PATH, and HOME variables,
j) Displays “Goodbye + current time” to the user.
#!/bin/bash
echo "Hello $USER"
cat /etc/os-release
echo "The group id of the current user $(id -g)"
echo -e "The current time \n$(date)"
ls -al ~
echo $TERM
echo $PATH
echo $HOME
echo "Goodbye $(date) $USER"
Exercise 6: Write a shell script named test.sh (with with any number of
parameters and parameter list) that displays the following information:
- Program name
- Number of command line parameters
- First command line parameter entered by the user
- All command line parameters entered by the us
#!/bin/bash
echo "Program name: $0"
echo "Number of command line parameters: $#"
echo "First command line parameter: $1"
echo "All command line parameters: $@"
Exercise 7: Write a shell script to convert a decimal integer to hexadecimal.
The shell script takes a command line parameter which is a decimal integer
to convert. Hint: using bc command to convert from decimal to hexadecimal
#!/bin/bash
if [ -z "$1" ]; then
echo "Please provide a decimal integer to convert."
exit 1
fi
decimal=$1
hexadecimal=$(echo "obase=16; $decimal" | bc)
echo "Decimal: $decimal"
echo "Hexadecimal: $hexadecimal"
Exercise 8: Program a shell script to print the Fibonacci sequence up to the
nth number. The Fibonacci sequence has the following characteristics: - F(0)
= 0 - F(1) = 1 - F(n) = F(n-1) + F(n-2) with n > 1 ● Requirements: - Enter the
value n from the user. - Print the Fibonacci sequence from F(0) to F(n).
#!/bin/bash
echo "Enter a number n to display the Fibonacci sequence up to F(n):"
read n
if ! [[ "$n" =~ ^[0-9]+$ ]]; then
echo "Please enter a valid non-negative integer."
exit 1
fi
a=0
b=1
echo "Fibonacci sequence up to F($n):"
for ((i=0; i<=n; i++))
do
echo -n "$a "
next=$((a + b))
a=$b
b=$next
done