In: Computer Science
unix operating system
create the scripts in your Fedora Server Virtual Machine
1- Open the file lab1 located in the labs directory and create a script that prompts the user to enter name, address and phone #. All entered details are directed to file "info.txt" in the same directory
When display info.txt, it should look like this example:
Name: John Smith
Address: 31-10 Thomson Ave.
Phone: 718-482-0000
2- Open the file lab2 located in the labs directory and create a script to display the logged in user name and user UID
When you execute lab2, the output should look like this example:
Username: root
User ID: 0
I created the scripts in Kali Linux since I don't have Fedora Server Virtual Machine. But you need not worry as the steps provided will work in any Linux Operating System.
At first, I created the directory 'labs' with
the command: mkdir labs in the root directory or
the home directory.
We create the lab1 file , we use the command
touch lab1 . Now we create the script.
The first line of a bash script should always sstart with a #!/bin/bash which denotes that the script will use the interpreter as the bash shell. THis is very much important in create an linux script.
Now, we take the user input with the following command
read -p "Enter your name:" name which takes the
takes and put it in the variable 'name'
The same is done for inputs address and
number
Now we echo the values of $name, $address and $number to the
info.txt file which is created along.
We are done with the script now, it gets created with the inputs
given by us.
lab1.sh file:
#!/bin/bash
read -p "Enter your name: " name
read -p "Enter your address: " address
read -p "Enter your phone number: " number
echo "$name" >> info.txt
echo "$address" >> info.txt
echo "$number" >> info.txt
Here is the screenshot to the script and the output:
chmod -x make this script executable
chmod 777 makes the file/script read, write, and
execute permission for all the users.
2) We use echo $USER print the
logged in user and id -u to print numberic
id.
Make sure to use #!/bin/bash at the start of the
program.
With the help of echo command, we print the result.
lab1.sh file
#!/bin/bash
echo "Username:$(echo $USER)"
echo "User ID:$(echo $(id -u))"
Screenshot to the file:
Screenshot to the output and execution:
Always remember to give permissions to files and foldes with the chmod commnad in linux.
If you have any doubts, leave a comment below before rating. I'll happy to assist you further. Do upvote this if it helped you. Thank you