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)
12
  • 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. Dec 21 '19 at 13:51
  • The last command must be mv ex2 log Dec 21 '19 at 14:00
  • Thank you and what do you think of the script? Is it correctly written? Dec 21 '19 at 14:06
  • 3
    "Is it correctly written?" Did you try it? Does it work ;) ? Dec 21 '19 at 14:10
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.