Really having trouble with this Unix problem, any help would be great a) script1
ID: 3817587 • Letter: R
Question
Really having trouble with this Unix problem, any help would be great
a) script1.sh : Write a shell script that takes 2 arguments, the first one a filename and the second one a number. The script should print the filename to the screen ‘x’ times using a loop (where ‘x’ is the number of characters in the filename), and then should check to see if the second argument is a number less than 0. If it is, print out the message “Negative”. If it is not, print out the message “Positive”. Separate the looped messages from the last message you display with a blank line. HINT: There are several ways to get the length of a string, so do some searching to find one to use.
As an example, if you run your script using the command ./script1.sh test.sh 46 , your script should display to the screen the following:
test.sh
test.sh
test.sh
test.sh
Positive
Explanation / Answer
a) script1.sh
#! /bin/bash
file_name=$(echo $1 | cut -d . -f 1) 'read given flename and remove the file extension in the name which helps in count in the number of chracters in file name'
count=$(echo $file_name | wc -c) # counts number of chracters in file name excluding file extension
for ((i=1;i<count;i++)); do # for loop to repeat count of number of filename excluding file extension
echo "$1" # print filename i times
done
printf " " # prints newlines two times for distance between above and below output
if [ $2 -lt 0 ] # checks for number is lessthan 0 or not
then
echo "Negitive" # if number lessthan 0 then prints negative
else
echo "Positive" # if number greaterthan 0 then prints positive
fi
Sample Output1:
./script1.sh test.sh 46
test.sh
test.sh
test.sh
test.sh
Positive
Sample Output2:
./script1.sh sample.sh -5
sample.sh
sample.sh
sample.sh
sample.sh
sample.sh
sample.sh
Negitive
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.