This question is on linux system. Please create a bash script that has to be exe
ID: 3864249 • Letter: T
Question
This question is on linux system. Please create a bash script that has to be executable and must include everything asked for below
Please remember, the script has to be executable
#!/bin/bash/
- Create an I/O redirection to a file (send an output to any sort of file)
- Then pipe it into something (ex: grep, awk, cut...)
- Create a sybolic link
- Use Wild cards
- Create a user
- Add the user to multiple groups
- Create an alias for the script
- Must include Reademe.md file (describing the script, what the script does)
Please show all your work and teach me step by step how the script was made and I promise to give you a thumbs up :).
Thanks you in advance
Explanation / Answer
STEP1: Create a script file
On the terminal type the command " touch script.sh " i.e
$) touch script.sh
(Remember "$)" denotes the terminal prompt , you must only type "touch script.sh ")
It's a general convention to end a script name with ".sh" , although it is not necessary.
STEP 2)
Write your code inside the script .
STEP 3)
Make the code executable using following command :
$) chmod +x script.sh
Please find the below the contents of the script. We use "#" for commenting in shell scripts. Any commented line is not executed , although there is an exception. The first line of the script generally starts with a "#!" followed by the script interpreter path . In your case the script has to be executed by the bash shell and it's location is "/bin/bash".
script.sh
#!/bin/bash
FILENAME="./file.txt"
#IO redirection
#In general the echo command prints the output to the screen .
#Using ">" followed by a filename, redirects the "echo" command output to the "input.txt" file.
# ">" creates a new file . If the file already exists it will empty it and then write the contents to that file.
echo "I am writing a bash script." >$FILENAME
#Following is also redirection but in this case the value is appended to the end of the file, for this we use ">>"
echo "I am writing again to the file." >>$FILENAME
#Piping the output
#Let's pipe the output to the grep command. Pipe works for the commands which read and write to the standard output.
#so when we write
# (command1) | (command2)
# So the standard output of the command1 will act as standard input for command2. Whatever "command1" whishes to print on the screen
# goes to the "command2" (is it is trying to read).
# "grep" commands tries to search the given word in the files specified. If no file is specified the grep reads from standard input.
# Also the "echo" command writes to the standard output . If we pipe those two commands then the output of "echo"
# will be input for "grep". Now i am using "-e" option with echo so that it interprets ' ' as a newline character.
# Now echo outputs two lines to the standard output which is redirected as input to the grep command which searches occurence of
# word "bash" in each line and prints the lines which has "bash" in it.
echo -e "I am writing a bash script. Using pipe in my script" | grep "bash"
#"ln" command creates a symbolic link for a file. Symbolic links are kind of shortcuts to a file
#Below command creates a symlink named "file_sum" for "file.txt"
ln -s $FILENAME "file_sym"
#Wild card characters has special meaning in the program
#List all the diretories in the current directory
# "^" wild character means grep will search the word (in this case 'd') in the beginning of each line only.
# if you see the output of "ls -l" each entry for a directory starts with 'd'
ls -l | grep "^d"
#list all files ending with ".sh" in current directory.
# "*" wild character means 0 or more characters
ls *.sh
#Inorder to create a user . You must be root or need sudo access otherwise the command will fail. Use the following command to create a user
useradd newLinuxUser
#Before adding user to any group let's first create two new groups. Remember you need to be a root user or need "sudo" access to perform this task
groupadd group1
groupadd group2
#Adding new user to group1 and group2
usermod -aG group1 newLinuxUser
usermod -aG group2 newLinuxUser
#Following is the command to create alias, but remember this alias won't work once the script execution is over.So you have to type the below command on the terminal
#to test it . To test the alias , just type the alias name and hit enter.
alias script="./script.sh"
README.md
---------
ABOUT
---------
This is a bash script to be executed in a linux environment.
The script shows basic concepts of shell scripting like
redirection, wild cards, user creation etc.
---------
EXECUTION
---------
User must be root user or have sudo access.
sudo ./script.sh
-------------
FUNCTIONALITY
-------------
1) Script first shows redirecting output to a file.
2) Then script shows how to use pipes '|' in shell scripting
3) Then the script creates a symlink using "ln"
4) The script shows the working of wild cards.
5) Creates a new User "newLinuxUser"
6) Creates new groups "group1" and "group2" and adds user "newLinuxUser" to these
groups.
-----------------------------------------
CLEANUP BEFORE EXECUTING THE SCRIPT AGAIN
-----------------------------------------
Do following things:
Change the username that you are creating otherwise script will throw an error telling
the user exists.
Change the group names too
remove the symlink using " rm -f file_sym"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.