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
Examand enter this directory. - While in the
Examdirectory, create a file calledsc1. - Open the
sc1file 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
ex1file. - Rename the
ex2file and name itlog.
Answers :
mkdir /home/student/Exam/+cd /home/student/Examtouch sc1We should type
vim sc1and then type the following:#!/bin/bash while dir /home/student/Desktop/; do touch "$dir/ex1" touch "$dir/ex2" touch "$dir/ex3" touch "$dir/ex4" donecp -a /home/student/Desktop/. /home/student/Exam/ls -l ex1(Corrected)mv ex2 log(Corrected)
whileloop here (you could just usetouch /home/student/Desktop/ex{1..4}), and it's unclear whichex2file 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 ex1based on exercise instructions – kenlukas Dec 21 '19 at 14:31while dir /home/student/Desktop/you run the commanddiron file/home/student/Desktop/. By chance,diris a valid command and returns a zero exit status if the file exists - so the body of thewhileloop gets executed. However/home/student/Desktop/does not get assigned to the variablediras you seem to be expecting. You can't just substitutewhile [BOOLEAN]; doin place offor word in [LIST]; doin the way you are trying. (Aforloop would make much more sense in this context - which is perhaps why you are getting confused.) – steeldriver Dec 21 '19 at 15:11ex1is executable? Don't you mean you need to check thatsc1is executable? – terdon Dec 21 '19 at 15:13