Greetings dear community, I would love your assistance regarding this small bash-based exercise. I have completed the exercise but I am not sure if it is entirely correct. Kindly advise for any improvements or possible errors.
Exercise:
- Create a directory called
Exam
and enter this directory. - While in the
Exam
directory, create a file calledsc1
. - Open the
sc1
file and write a bash script that will - "Using the while loop, create four files (ex1, ex2, ex3, ex4) in the/home/student/Desktop/
Directory. - Copy these files into the
/home/student/Exam/
Directory. - Check/Verify if we have the permissions to run the
ex1
file. - Rename the
ex2
file and name itlog
.
Answers :
mkdir /home/student/Exam/
+cd /home/student/Exam
touch sc1
We should type
vim sc1
and then type the following:#!/bin/bash while dir /home/student/Desktop/; do touch "$dir/ex1" touch "$dir/ex2" touch "$dir/ex3" touch "$dir/ex4" done
cp -a /home/student/Desktop/. /home/student/Exam/
ls -l ex1
(Corrected)mv ex2 log
(Corrected)
while
loop here (you could just usetouch /home/student/Desktop/ex{1..4}
), and it's unclear whichex2
file should be renamed (at that point there should be two of them). – Kusalananda Dec 21 '19 at 13:50mv ex2 log
– Romeo Ninov Dec 21 '19 at 14:00touch "$dir/ex1" touch "$dir/ex2" touch "$dir/ex3" touch "$dir/ex4" done - It should work, but it keeps giving me permission denied cannot touch '/ex1' and the same goes for other three. – Michael Scofield Dec 21 '19 at 14:27
ls -l ex1
based on exercise instructions – kenlukas Dec 21 '19 at 14:31while dir /home/student/Desktop/
you run the commanddir
on file/home/student/Desktop/
. By chance,dir
is a valid command and returns a zero exit status if the file exists - so the body of thewhile
loop gets executed. However/home/student/Desktop/
does not get assigned to the variabledir
as you seem to be expecting. You can't just substitutewhile [BOOLEAN]; do
in place offor word in [LIST]; do
in the way you are trying. (Afor
loop would make much more sense in this context - which is perhaps why you are getting confused.) – steeldriver Dec 21 '19 at 15:11ex1
is executable? Don't you mean you need to check thatsc1
is executable? – terdon Dec 21 '19 at 15:13