In: Computer Science
CIT 371 Lab 12: shell scripting
Start VMware, your VM and log in as Student. cd to your home directory and create a subdirectory called scripts. cd to that directory. You can use gedit or some other editor but you need to get used to vi, so I strongly recommend that you use vi. Remember before running any new script that you will need to modify its permissions to be executable. Use either 745 or 755.
#!/bin/bash
echo You are $USER
echo Your home directory is $HOME
echo Your home directory consists of `du –sH ~`
Run the script by typing ./script1 <enter>. Alter your script so that the output of each echo statement is redirected to the file info.txt. How did you do this?
#!/bin/bash
echo What is your name?
read NAME
echo What is your username?
read USERNAME
echo Hello $NAME, your home directory contents and size:
Complete this script by adding instructions to output this user’s home directory contents (using ls) assuming that the user’s home directory will be /home/$USERNAME as well as using the du statement from script1 to output the disk usage of the user’s home directory. When done, add this script to your answer file.
#!/bin/bash
if [ $# -ne 2 ]; then echo Illegal input
elif [ $1 –gt $2 ]; then echo $1 is greater
else echo $2 is greater
fi
Save and run the script providing it no parameters, two parameters of 5 and 10, two parameters of 10 and 5, and two parameters of 5 and 5. What does [ $# -ne 2 ] mean? Do we need this in the script?
#!/bin/bash
read –p “Enter the number you seek ” NUM
for VALUE in $@; do
if [ $VALUE –eq $NUM ]; then COUNT=$((COUNT+1)); fi
done
echo $NUM appeared $COUNT times
Run script5 passing it the list of numbers 5 10 6 12 5 18 10 4 19 21 5 12 18 22 and when prompted, input 5. Rerun the script inputting 18 instead. Rerun the script inputting 23 instead. What outputs did you get for each input?
Shut down your VM, exit VMware and submit your lab report.
Script 1
***********
bash script1.sh > info.txt
Script 2
**********
#!/bin/bash
echo "What is your name?"
read NAME
echo "What is your username?"
read USERNAME
echo "Hello $NAME, your home directory contents and size:
`ls`"
echo
echo "size: `du -sH /home/$USERNAME`"
Modified
#!/bin/bash
echo "Hello $1, your home directory contents and size:
`ls`"
echo
echo "size: `du -sH /home/$2`"
Script 3
**********
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Illegal input"
elif [ $1 -gt $2 ]
then
echo "$1 is greater"
else
echo "$2 is greater"
fi
What does [ $# -ne 2 ] mean
This means if there must be 2 arguments if not then condition will be false
Script 4
*********
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Illegal input"
elif [ $1 -gt $2 ]
then
echo "$1 is greater"
elif [ $1 -eq $2 ]
then
echo "$1 equals $2"
else
echo "$2 is greater"
fi
Script 5
************
#!/bin/bash
read -p "Enter the 1st number " NUM1
read -p "Enter the 2nd number " NUM2
for VALUE in $@;
do
if [ $VALUE -ge $NUM1 ] && [ $VALUE -le $NUM2 ]
then
COUNT=$((COUNT+1))
fi
done
echo
echo "$NUM appeared $COUNT times"
#!/bin/bash echo "enter size of an array" read n #taking input from user for((i=0;i<n;i++)) do echo " enter $((i+1)) number" read nos[$i] done #printing the entered number echo "number entered are" for((i=0;i<n;i++)) do echo ${nos[$i]} done #main loop small=${nos[0]} greatest=${nos[0]} for((i=0;i<n;i++)) do #logic for smallest number if [ ${nos[$i]} -lt $small ]; then small=${nos[$i]} #logic for greatest number elif [ ${nos[$i]} -gt $greatest ]; then greatest=${nos[$i]} fi done #printing smallest and greatest number echo "smallest number in an array is $small" echo "greatest number in an array is $greatest"
if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and i can edit and change the answers if you argue, thanks :)