Bash Shell Programming assignment.

Please take a look I am stuck on step 4

  1. The problem statement, all variables and given/known data:
#!/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.
  1. Relevant commands, code, scripts, algorithms:

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

### 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
  1. 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).

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:

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

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.

1 Like

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

Try these commands at the command line and see if they don't help you along a bit:

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

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

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:

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?


  1. 0-9 ↩ī¸Ž

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

Because this is an assignment I don't think anybody here will hand you the solution. I'm sure if you were to post what you've tried, or even pseudo code, people would offer suggestions as to what might be causing you to get stuck.

1 Like
num="109"
for ((i=0;i<3;i++)); do
    echo ${num:$i:1};
done

Assignment said use cut ? hmm.. see agama's post.

1 Like

Thanks for reply, but I need step 4 solution my friend.
if possible....

Does anybody have a hint for step 7 part (1)
### STEP 7
###=================================
### Part (1):
### Display all the non-hidden files in your ~/a2/Pictures that have a
### filename that consists of one digit only
I tried ls | grep [0-9] and [0-9]{1} and so on but it always displays one and more digit.

Thanks

echo path/to/[0-9]

1 Like

Google makes miracles )

Here one of the solution for #1

read -p "Write your student ID: " ID              #write your variable $ID, you can use yours
if ! echo $ID | egrep "^[0-9][0-9][0-9]$"          
then
   echo "Write your ID, I'm NOT joking"
   exit 1
fi
1 Like

My understanding of the assignment was that the ID was passed as $1...

When this script is run, one parameter/argument is passed to the script.

The step4 solution you desire is nothing more than the loop

id=109
for ((i = 1; i <= 3; i++)); do
        digit=$(echo $id | cut -c${i})
done

with the copy inside of it

cp ~a2/Pictures/linux-pic${digit} ~/public_html/
1 Like