NOTE: The first line of your script should be the following: #!/bin/bash This mu
ID: 3873189 • Letter: N
Question
NOTE: The first line of your script should be the following:
#!/bin/bash
This must be exactly the first line of your script.
For ODP404, you need to write a script that accepts 2 command line arguments
(in these examples, I'm assuming your script is named "script"):
script word number
should print the given word, once per line, a total of "number" times:
The following command:
script Hello 5
would produce 5 lines of output:
Hello
Hello
Hello
Hello
Hello
You may assume both the word and the number will be present (don't worry about
the case where the user forgets one or both of these). If numbers is less than
1, your script shouldn't print anything.
After you write your script and test it, assess it using the assess command.
For-Loops
To do this, you can use a bash for loop. The syntax is similar to C:
for ((i=0;i<10;i++))
do
things to do
possibly more things to do
and so on
done
The above code will repeat the statement between "do" and "done"
and will increment the value of the variable i after each iteration.
Play around with this to make sure you understand for loops; refer to your
CSE 121 notes or other resources (including me!) if you're unsure how a
for loop works.
Try things like this too:
for ((i=5;i>0;i--))
do
echo i=$i
done
Explanation / Answer
#!/bin/bash
for ((i = 1; i <= $2; i++)); do
echo $1
done
Explaination:
$1 is the first argumant
$2 is the second argyment
Save the above script in file script.sh and run ./script Hello 5
Hello
Hello
Hello
Hello
Hello
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.