Create 3 shell scripts, one for each of the following tasks. Put each one in a n
ID: 3817043 • Letter: C
Question
Create 3 shell scripts, one for each of the following tasks. Put each one in a new file, and name them ‘script1.sh’, ‘script2.sh’, and ‘script3.sh’. Make sure to run the command ‘chmod 777 script1.sh script2.sh script3.sh’ to give full permissions to read, write, and execute each script, and then submit each of the 3 files along with this assignment using handin
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
b) script2.sh : Write a shell script that prints out the numbers of the Fibonacci series. This series of numbers is one where every number is the sum of the previous two numbers before it. For example, the first 10 number of the series is {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}. The series is infinite, so assume that one command line argument is given to your script which is the total number of Fibonacci numbers to display. If a ‘1’ is given, have it print off just the first number. If a ‘2’ is given, have it just print off the first two numbers. If a ‘3’ or bigger is given, have it print off the first two numbers, then have your code calculate the rest automatically. Assume that a logical number will always be given to you (don’t worry about checking to see if the number is bigger than 0, for example).
As an example, if you run your script using the command ./script2.sh 5 , your script should display to the screen the following:
0
1
1
2
3
) script3.sh : Write a shell script which asks the user to enter two numbers, the first one a being smaller than the second number. If the first number entered is bigger than the second, print off the message “The first number must be smaller than the second!” and stop the script. If the first number is smaller than the second, have the script list all ordinary files with sizes between a certain range in a given directory. Assume that the directory is specified when the script is run as the first and only command line argument. Display only the files between the minimum size (in number of bytes) and the maximum size (in number of bytes) that the user entered in (minimum size is the first number, and maximum size is the second number). Thus, your script must output under the directory (specified by the first argument $1) all the files whose sizes are no less than the minimum size (the smaller number) and no greater than the maximum size (the bigger number) in bytes. Each file’s name must be displayed along with its size on one line. Only consider files in the directory given, not in any subdirectories. See the example below to see how to have your program display information.
As an example, suppose in the directory /opt/new/ there are four ordinary files: a.txt, b.txt, c.txt and d.txt, and their sizes are 3, 9, 14 and 20 bytes, respectively. If you run your script using the command ./script3.sh /opt/new/ , and you enter in the values 5 and 15 when asked, you should get the exact same following output:
Enter a number: 5
Enter in a bigger number: 15
The files in /opt/new/ that are between 5 and 15 bytes are the following:
b.txt is 9 bytes
c.txt is 14 bytes
Explanation / Answer
# script1.sh
file_name=$1
filename_without_ext="${file_name%.*}" # Fetching only file name with out extension
file_name_length=${#filename_without_ext} # Getting number of chars in the file name
for i in `seq 1 $file_name_length` # For loop to iterate
do
echo "$1"; # Printing the file name
done
if [ $2 -lt 0 ] # check the number is less than 0
then
echo "Negative"
else
echo "Positive"
fi
# Script2.sh
n1=0
n2=1
Max=$1 # Taking input arguments
for i in `seq 1 $Max` # for loop
do
echo "$n1 " # printing fibbonaci series
n3=$((n1+n2))
n1=$n2
n2=$n3
done
#Script3.sh
echo " Enter a number: "
read min
echo " Enter a bigger number: "
read max
if [ $min -gt $max ] # checking if min is greater than max
then
echo "The first number must be smaller than the second!"
exit; # then exit
else
dir=$1
echo The files in $dir that are between $min and $ max bytes are the following:
for filename in $dir/* # iterating all files in dir
do
if [ -f "$filename" ]; then # Checking if it is regular file
size=$(wc -c <"$filename") # Fetching file size in bytes
if [ $size -ge $min ] && [ $size -le $max ]; # Comparing file sizes with min and max
then
echo $filename is $size Bytes
fi
fi
done
fi