In: Computer Science
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created
1. Open a new file script creafile.sh using vi editor
# vi creafile.sh
2. Type the following lines
#!/bin/bash
echo ‘enter a file name: ‘
read FILENAME
touch $FILENAME
echo “$FILENAME has been created”
3. Add the execute permission
4. Run the script #./creafile.sh
5. Enter the file name you want to create
6. Check whether file is created
-------------------------------------------------------------------------------------------------
Task 3.4 Challenge Exercises
a.Write Script to see current date, time, username, and current directory.
b.Take the script from Task 2.5 and write it so it does not create a file if no input is received.
c. In this exercise, you will create a script based on case statement.
d. Write a Shell using for loop to print three strings.
e. Write a bash Script to reverse a given positive integer.
Task 2.4
***********
#!/bin/bash
echo 'enter a file name: '
read FILENAME
touch $FILENAME
echo "$FILENAME has been created"
Task 3.5
**********
#!/bin/bash
i=0
while [ $i -ne 5 ]
do
echo "1.Write Script to see current date, time, username, and
current directory"
echo "2.Take the script from Task 2.5 and write it so it does not
create a file if no input is received."
echo "3. Write a Shell using for loop to print three
strings."
echo "4. Write a bash Script to reverse a given positive
integer."
echo "5. exit"
echo "Enter Your choice: "
read INPUT_STRING
case $INPUT_STRING in
1)
echo "Current Date/Time is: "
`date`
echo "Current user is: "
`whoami`
echo "Current directory is: "
`pwd`
;;
2)
echo 'enter a file name: '
read FILENAME
if [ -z "$FILENAME" ]
then
echo "you have
not provided any file name"
exit
else
touch $FILENAME
echo "$FILENAME has been
created"
fi
;;
3)
echo "For loop to print string 3
times:"
for ((a=0;a<3;a++))
{
echo "Hello
World..."
}
;;
4)
read -p "Enter a number to reverse:
" num
echo $num | rev
;;
5)
echo "Bye"
i=5;
exit
;;
*)
echo "Sorry, I don't
understand"
;;
esac
done
if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)