0

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:

  1. Create a directory called Exam and enter this directory.
  2. While in the Exam directory, create a file called sc1.
  3. 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.
  4. Copy these files into the /home/student/Exam/ Directory.
  5. Check/Verify if we have the permissions to run the ex1 file.
  6. Rename the ex2 file and name it log.

Answers :

  1. mkdir /home/student/Exam/ + cd /home/student/Exam
  2. touch sc1
  3. 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
    
  4. cp -a /home/student/Desktop/. /home/student/Exam/

  5. ls -l ex1 (Corrected)
  6. mv ex2 log (Corrected)
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 2
    I don't really see the use for the while loop here (you could just use touch /home/student/Desktop/ex{1..4}), and it's unclear which ex2 file should be renamed (at that point there should be two of them). – Kusalananda Dec 21 '19 at 13:50
  • The one that was coped to /home/student/Exam/. It is a template exercise, it does not make much sense. My lecturer wants me to get better in scripts :D that's why the while loop is there. – Michael Scofield Dec 21 '19 at 13:51
  • The last command must be mv ex2 log – Romeo Ninov Dec 21 '19 at 14:00
  • Thank you and what do you think of the script? Is it correctly written? – Michael Scofield Dec 21 '19 at 14:06
  • 3
    "Is it correctly written?" Did you try it? Does it work ;) ? – steeldriver Dec 21 '19 at 14:10
  • #!/bin/bash while dir /home/student/Desktop/; do
    touch "$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
  • Your #5 is listing the wrong file, should be ls -l ex1 based on exercise instructions – kenlukas Dec 21 '19 at 14:31
  • Thanks for pointing it out. – Michael Scofield Dec 21 '19 at 14:39
  • "It should work" no, it shouldn't. In while dir /home/student/Desktop/ you run the command dir 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 the while loop gets executed. However /home/student/Desktop/ does not get assigned to the variable dir as you seem to be expecting. You can't just substitute while [BOOLEAN]; do in place of for word in [LIST]; do in the way you are trying. (A for loop would make much more sense in this context - which is perhaps why you are getting confused.) – steeldriver Dec 21 '19 at 15:11
  • Also, are you sure you need to check that ex1 is executable? Don't you mean you need to check that sc1 is executable? – terdon Dec 21 '19 at 15:13
  • Yes. Thanks for your help. – Michael Scofield Dec 21 '19 at 15:16
  • Nowhere does it say that the script you write should ever be executed. The three last steps are void. – Kusalananda Dec 22 '19 at 09:18

2 Answers2

2
  1. Create a directory called Exam and enter this directory.

    mkdir Exam && cd Exam

  2. While in the Exam directory, create a file called sc1.

    touch sc1

  3. 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.

    See the first example here Create a variable called i and assign it 1, then start looping, and do it while i isn't lower than 5 (lower than means until 4) - or you could use -le 4 for lower or equal 4. Add 1 to the last i value before next iteration.

i="1"

while [ $i -lt 5 ]
do
  touch "ex$i"
  i=$[$i+1] 
done
  1. Copy these files into the /home/student/Exam/ Directory.

    cp ex* /home/student/Exam/

  2. Check/Verify if we have the permissions to run the ex1 file.

    Check by tunning ls -l /home/student/Exam/ex1

    Fix by running chmod 700 /home/student/Exam/ex1

  3. Rename the ex2 file and name it log.

    mv /home/student/Exam/ex2 /home/student/Exam/log

nnsense
  • 389
1
i=1
while (( i <= 4 ))
do
   echo "ex$(( i++ ))"
done

This is the "nicest" while loop I can make. A for loop can take all three expressions:

for (( i = 1; i <= 4; i++ ))
do
   echo "ex$i"
done

Or like this, avoiding the (( double parens )) altogether.

for i in 1 2 3 4
do
   echo "ex$i"
done

This is a in-between solution: it is easier to say "1,2,3,4" than "from 1 to 4".

But touch "ex1"; touch "ex2"; touch "ex3" ... is not a good idea in general - and in particular if you are told to use a loop. Being lazy is important, but a programmer must also be too proud to copy paste (more than once).

for i in 1 2 3 4
do
   touch "/home/student/Desktop/ex$i"
done

This should be the complete loop. If ever you need more or different numbers (or letters/strings), you can easily change it.

And since we have left the while loop anyway, let me add the shortest kind of "loop", brace expansion:

touch /home/student/Desktop/ex{1,2,3,4}

or

dir=/home/student/Desktop
touch $dir/ex{1..4}

Tell me master can you answer a question:
Is it the fingers or the brain that you're teaching a lesson? 
                                       (White Stripes)