Bash Shell Programming assignment.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Bash Shell Programming assignment.
# 1  
Old 07-31-2011
Bash Shell Programming assignment.

Please take a look I am stuck on step 4

1. The problem statement, all variables and given/known data:
Code:
#!/bin/bash
### ULI101 - ASSIGNMENT #2 (PART A) - DUE DATE Wed, Aug 3, 2011, before 12 midnight.
###====================================================================================
### Create a2 directory in your home directory. Do everything for this
### assignment in your a2 directory.
### Create a directory called "Pictures" in your a2 directory. Copy all files from
### URL to your ~a2/Pictures directory.
### This directory contains 10 image files, along with a text file called a2 (this file),
### which you will edit using vi, and add commands that do what is being asked in the comment lines
### When this script is run, one parameter/argument is passed to the script.
### This parameter/argument should consist of the last 3 digits of your student id
### STEP 1
### ======================
### Make sure that the parameter/argument entered by the user consists of 3 numbers.
### If its not 3 numbers, then give appropriate error message and exit with exit status of 1.
### If the parameter entered was a 3 digit number, then Display it
### STEP 2
###=======================
### Check if ~a2/Pictures directory exists in your home directory.
### If this directory is not found, give an appropriate error msg and exit with exit value of 2
### If this directory exists, then go to the next step and do not display any error msg
### STEP 3
### ======================
### List all files that exist in your ~a2/Pictures directory
### STEP 4
###==========================
### Use each of the 3 digits that you entered as parameter
### (last 3 digits of your student number) and copy the appropriate files
### from the Pictures directory to ~/public_html directory
### These will be used in part B of this assignment when you create your webpage.
### If your last 3 digits were 123, then you will copy files starting with:
### linux-pic1, linux-pic2, linux-pic3 to your ~/public_html directory
### HINT: use 'cut' command to extract each of the 3 digits
### HINT: use a loop to copy the 3 appropriate files
### STEP 5
### ============================
### In ~/a2/Pictures directory, set permissions of all image files
### that start with 'linux-pic', such that 'owner/user' has read
### and write permission, 'group' has none, and 'others' have read permission only.
### Once you have changed the permissions, list them so it shows what
### permissions they have now
### STEP 6
###==============================
### Create 3 empty files in your ~/a2/Pictures directory, each corresponding
### to one of the 3 digits from your student-id that you passed as a parameter/argument
### STEP 7
###=================================
### Part (1):
### Display all the non-hidden files in your ~/a2/Pictures that have a
### filename that consists of one digit only
### Part (2):
### Now use a command to do a count of files you found in part (1)
### STEP 8
###===============================
### Create a basic webpage in ~/a2 directory
### This can be a basic 'html' page for now, and in part B of this assignment,
### you will convert this into an 'xhtml' page
### This webpage should have :
### - a title (use <title> tag) that says 'My webpage'
### - a 'body' (use <body> tag) in which should say 'This is my basic webpage'
### Your webpage should be called "mypage123.html" where you need to
### replace 123 with the last 3 digits of your student number
### HINT: use 'here document' to write data into an html file
### HINT: Before you create the webpage, make sure you delete the
### existing webpage if it exists
### STEP 9
###============================
### Make sure the mypage123.html has been created
### and display the file pathname for it
### Now display the contents (code) of the webpage that you just created
### STEP 10
###==============================
### Now in your ~/public_html directory, please create a subdirectory
### called assign2, into which you will copy certain files
### (first check if this directory exists already, and if it does, delete it)
### Now copy the following files from your ~/a2 directory to ~/public_html/assign2:
### - all files whose filename starts with 'linux-pic' followed by one of the 3 digits
### that make up last 3-numbers of your student id (eg. if last 3 digits of your
### student id are 1, 2 and 3, then you will copy linux-pic1*, linux-pic2* and linux-pic3*
### - the html file that you created in the step above
### Now list all files that you have copied into ~/public_html/assign2
### Submit your assign2 (part A) by doing the following:
### 1) Put contents of your script into 'myresult' file (HINT use 'cat' and redirection)
### 2) Now run your assignment#2 with the appropriate parameter (last 3 digits of your student number)
### and redirect the output and append to 'myresult'.
### 3) Now that 'myresult' contains your code for the script as well as the result of
### running your script, you can send it to your professor by using the 'mail' command.
### 4) You must use the following subject line: ULI101A Assign2
###
### The assign2 (Part B) will be released next week and in that you will use the webpage
### that you created through this script, and will modify it.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):

Code:
### STEP 1
### ======================
#set my student ID

set studentid=106

echo -n "Enter the last 3 digits of your student id:"

read studentid


if test "$studentid" -eq 106

        then

echo $studentid

elif [[ "$studentid" != ^[0-9][0-9][0-9]$ ]];

        then

echo "Please enter the last 3 digits"

exit 1

fi


### STEP 2
### =======================

dir=a2/Pictures

if [ -d "$dir" ];
        then
echo -n "$dir exists. 
"

elif [ ! -d "$dir" ];
        then
echo -n "$dir doesn't exist"

 exit 2

fi

### STEP 3
### ======================

ls ~/a2/Pictures

### STEP 4
###==========================
1=~/a2/Pictures/linux-pic1-clustertux.gif
2=~/a2/Pictures/linux-pic0-baby-penguin.png
3=~/a2/Pictures/linux-pic6-business.jpg

for first in $1
do 
cp $1 ~/public_html
done


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):Seneca College, Toronto, Don Frray, ULI101


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by pludi; 07-31-2011 at 06:35 PM..
# 2  
Old 07-31-2011
These are my observations about your attempt:

In step 1, unless my versions of Kshell and Bash are way out dated, I do not believe this test will work:

Code:
elif [[ "$studentid" != ^[0-9][0-9][0-9]$ ]]

Both Kshell and Bash treat the right hand side of the expression as a pattern, but NOT a regular expression. You're on the right track, but regardless of the value in studentid, this test will always be true.

In step 2, this dir=a2/Pictures is OK, but if the script is run from another directory your later test will fail. You might think about changing the current directory to your home directory early on.

Step 4....
First, 1 is not a valid variable name so the assignment statement
Code:
1=~/a2/Pictures/linux-pic1-clustertux.gif

is not correct. Same for the other two assignments.

From the instructions you should not be hard coding the filenames that you are to copy. You need to disect the user number entered in step 1 and build the source filename using each digit. The hint given in the instructions should be a big clue that the cut command will make it easy to extract the desired digit (first, second or third) and then you need to use the extracted digit to build the source filename to copy.

Take a look at the manual page for the cut command and pay particular attention to the -c option.

I'm guessing that there are only 10 picture files (linux-pic0... linux-pic9) in the source directory, but if you think of there being more than one linux-pic0 file (maybe linux-pic0-a and linix-pic0-b) and how you might copy all of those using one command, you might see how you can specify the source filenames on your copy command without needing to code the whole pathname.

Sorry if this is difficult to understand, but as this is an assignment I only want to provide you with some clues that might get you onto the right path.
This User Gave Thanks to agama For This Post:
# 3  
Old 07-31-2011
Thanks for the response.
The step 1 works fine and I tested the command by saving the whole thing as a file with .sh extension.
yes, you are right and I have files from 0 to 10 correspond to the last 3 digits of my student id which is 106.
The problem is i don't know what to do for step 4!
I am confused with cut command and everything seems complicated. this is almost the end of semester and I have few days to complete this assignment!
Would you please give me more hint for step 4
Thanks
# 4  
Old 07-31-2011
Try these commands at the command line and see if they don't help you along a bit:

Code:
stuff="abcdefghi"
echo $stuff | cut -c 2
letter=$(echo $stuff| cut -c 3)
echo "3rd letter is: $letter"

These 2 Users Gave Thanks to agama For This Post:
# 5  
Old 08-01-2011
Great hint. I am feeling better now.
Thanks again

---------- Post updated 08-01-11 at 12:36 PM ---------- Previous update was 07-31-11 at 07:35 PM ----------

Hi
I can't write this loop! I did my best and still have problem on this step! could you give me the solution fro step 4.
Thanks
# 6  
Old 08-01-2011
Hi, I am in the same class as almirazee (found this forum through google). I'm having trouble with Step 1 and elected to go a simpler route than him. Here's my attempt:


Quote:
echo "Please enter a 3 digit number"
read numbers

if [ "$numbers" = "^[0-9][0-9][0-9]$" ]
then
echo $numbers
else
exit 1
fi
I've tried '==', -eq, quotes, no quotes and permutations of these. The condition always equals false and goes to the "else" statement. I know this because when I add "echo $numbers" above the "exit 1" in the "else", it prints it out no matter what the input from the user.... without it, it just goes back to the prompt. Similarly, if I change the condition to "!=" the condition is always true no matter the input

I know it's a syntax thing, I've got the logic part (I'm in programming). So where am I going wrong?

Last edited by moe45673; 08-01-2011 at 02:10 PM..
# 7  
Old 08-01-2011
Hi there
Im in section B , ULI 101
Here is the possible solution of a step 1

27 echo "Enter the last tree digints of your student id:"
28
29 read num
30
31 if [[ $num =~ ^[[:digit:]][[:digit:]][[:digit:]]$ ]]; then
32 echo "Your studID = $num"
33 else
34 echo ERROR! YOU MUST TREE TYPE DIGITS ONLY!
35 exit 1
36 fi
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. Shell Programming and Scripting

Loosing trailing new line in Bash var assignment

I have come across a weird behaviour in bash and would love to get to the bottom of it. If I execute echo -e "\na\nb\nc\n" at the command line, I get: a b c However, if I wrap it in an assignment such as: A="$( echo -e "\na\nb\nc\n" )"then I get a b cIt doesn't show very well,... (4 Replies)
Discussion started by: jamesbor
4 Replies

3. Shell Programming and Scripting

Bash variable assignment question

Suppose I have a file named Stuff in the same directory as my script. Does the following assign the file Stuff to a variable? Var="Stuff" Why doesn't this just assign the string Stuff? Or rather how would I assign the string Stuff to a variable in this situation? Also, what exactly is... (3 Replies)
Discussion started by: Riker1204
3 Replies

4. Shell Programming and Scripting

Chunks of bash shell programming

I am going to provide a chunks of codes that I do not understand. Please help with them in a layman's terms. 1) ${DEBUG:-0} -------------------------------------------------------------------------- 2) print "${1}" ... (7 Replies)
Discussion started by: lg123
7 Replies

5. Shell Programming and Scripting

Making a bash script and small C program for a homework assignment

Here's the assignment. I'll bold the parts that are rough for me. Unfortunately, that's quite a bit lol. The syntax is, of course, where my issues lie, for the most part. I don't have a lot of programming experience at all :/. I'd post what I've already done, but I'm so lost I really don't know... (1 Reply)
Discussion started by: twk101
1 Replies

6. Shell Programming and Scripting

CPU assignment bash script

Hi guys, I'm basically looking for some help with a bash script I've written. It's purpose is to assign process to individual CPU cores once that process hits 15% CPU usage or more. If it drops below 15%, it's unassigned again (using taskset). My problem is that I can't think of a way to... (2 Replies)
Discussion started by: mcky
2 Replies

7. UNIX for Dummies Questions & Answers

Help with date command in bash shell programming

Doubt #1 I have a program that I want the user to input date. When the user inputs the date, is it able to format it to system date dd-mm-yyy and then echo the value into a text file? Doubt#2 If the above is not going to work, I tried to have the system date appear in the user input field and... (6 Replies)
Discussion started by: frossie
6 Replies

8. Shell Programming and Scripting

shell programming assignment

I have a very tough shell program to do. Here is the assignment: Write a non-interactive script that takes in any number of directory names as arguments and calculates and outputs the total number of blocks of disk space occupied by the ordinary files in all the directories. For example, the... (0 Replies)
Discussion started by: Jake777
0 Replies

9. Shell Programming and Scripting

variable assignment in new shell

Hi, I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments. I noticed that the assignment is not working. Please see two samples below: Command 1: #>ksh "i=2;echo I is $i" Output: #>I is Command 2: #>ksh <<EOF > i=2 > echo I... (7 Replies)
Discussion started by: maverick80
7 Replies
Login or Register to Ask a Question