In: Computer Science
Linux
Given a passed parameter to a script, request a number from the user and print the parameter as many times as that number.
Source Code with comments for your better understanding-
#!/bin/bash #reading the value from the first positional parameter denoted by $1 parameter_read=$1 #readding number from the user read -p "Please input a number: " numberinput #printing out the number that was given by the user echo "You have typed this number ${numberinput}" #initializing a variable i with digit 1 i=1 #doing a while loop to start from the initialised value of variable i till the digit which was #given by the user while [ "${i}" -le "${numberinput}" ] do #printing out the parameter which was passed in the script echo "${parameter_read}" #incrementing the variable by adding plus 1 i=`expr ${i} + 1 ` done
Create a script of your own choice and paste the abovecode in that script
Then call the script in this way (given below) from your command line terminal
sh scriptname parametervalue
where scriptname will be scriptname of your choice and parametervalue will be the parameter name of your choice
In our script I have passed hello as parameter and have given a digit 7 as input
So output will be hello which will be printed 7 times in separate lines
Screenshot-