LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-16-2012, 10:42 AM   #1
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Question Autograder Shell Script-- hints on getting started?


Hello! I have an assignment to create a shell script that is an autograder for computer programs submitted by students. I am really confused on how to approach this..new to unix so I am learning :-)

Task 1:
A due date is specified in a command line argument so I need to compare the current time to what got passed in, if it is past the submission date, then I need to lock the submission directory by removing certain permissions and then copy over everything in the submission directory into a local directory.

I understand how to to recursively copy all of the files over, however, I am completely lost as to how I would be able to compare a date that was passed in as 3 separate arguments and the current time now which I would just like to use
Code:
date +%s
is there a way to convert the month date and time into a comparable format for what date returns?

Thanks guys!! I'll update on task 2 as soon as I can figure this part out...
 
Old 10-16-2012, 10:46 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,885

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by calvarado777 View Post
Hello! I have an assignment to create a shell script that is an autograder for computer programs submitted by students. I am really confused on how to approach this..new to unix so I am learning :-)

Task 1:
A due date is specified in a command line argument so I need to compare the current time to what got passed in, if it is past the submission date, then I need to lock the submission directory by removing certain permissions and then copy over everything in the submission directory into a local directory.

I understand how to to recursively copy all of the files over, however, I am completely lost as to how I would be able to compare a date that was passed in as 3 separate arguments and the current time now which I would just like to use
Code:
date +%s
is there a way to convert the month date and time into a comparable format for what date returns?
Thanks guys!! I'll update on task 2 as soon as I can figure this part out...
Check out the man page on the date command, and pay particular attention to the +FORMAT switch. It has LOTS of options. If it was me, I'd convert the dates into epoch-time (the %s switch for formats), compare them, and then change them back to human-readable format.
 
Old 10-19-2012, 05:44 PM   #3
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Update on date questions

Hello! So after much reviewing and reading, this is what I have conjured up..thanks for the hints! I am not positive that my output formatting actually works though since I have tried to set it to seconds since the epoch...I found this side note online which makes me wonder..it is one of the output format options listed in the man pages.
Quote:
%s seconds since 00:00:00, Jan 1, 1970 (a GNU extension)
Note that this value is defined by the localtime system
call. It isn't changed by the `--date' option.
Does this mean that I cannot convert the string into that format?

Here is my code!

Code:
#get the current time in seconds since the epoch
timeNow="$(date +%s)"

#concatentate all of the time parts into a single string
submissionTimeString=$month" "$day" "$hour" "$minute        

#set the submission time to the value that date returns
#options on date set output to seconds since epoch format from the specified string
submissionTime="date +%s -d "$submissionTimeTemp""
Does this make sense at all or have I completely gone down the wrong path?
Thanks guys!
 
Old 10-19-2012, 07:15 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Quote:
Originally Posted by calvarado777 View Post
I am really confused on how to approach this..new to unix so I am learning
Lets start with the obligatory:
Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }


Code:
# Your initial thought to convert any date to epoch was the right one.
# Like you express epoch with:
timeNow="$(date +%s)"
# you can also modify it using --date, for instance:
timePast=$(date +'%s' --date="yesterday")
# or:
wayLate=$(date +'%s' --date="54 weeks ago")
# ...which means you can have the user input some value:
# *let's skip error handling for now
echo "Enter date in YYYY MM DD format: "; read INPUT
dueDate=$(date +'%s' --date="${INPUT// /-}")
# so you can compare values.
 
Old 10-19-2012, 08:21 PM   #5
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Pressed for time

So I am starting to get closer to submission time and I ended up taking an easy way out for my time calculations...I will however be playing with your code above thank you!!! So this is where I am at so far..I have my times working correctly but I am now down to two errors when I am trying to change the permissions and when I am copying the directories. I am almost positive that it's a syntax error but I have been referencing my book and notes and it seems right...is there some rule that I am missing? I can't think of a reason as to why I wouldn't be able to use the directories stored in the variables...?

Thanks again guys!

Code:
#get the current time 
currentMonth="$(date +%m)"
currentDay="$(date +%d)"
currentHour="$(date +%I)"
currentMinute="$(date +%M)"

#get submission time
submissionMonth=$month
submissionDay=$day
submissionHour=$hour
submissionMinute=$minute

#if the submission time is less than the current time, sleep for 2 hours
if [ $currentMonth=$submissionMonth ]; then
        if [ $currentDay -lt $submissionDay ]; then
                sleep 7200
        else
                if [ $currentHour -lt $submissionHour ]; then
                        sleep 7200
                else
                        if [ $currentHour -ge $submissionHour ] && [ $currentMinute -ge $submissionMinute ]; then
                                chmod ugo-w "$submissionFolder"
                        else
                                sleep 7200
                        fi
                fi
        fi
fi

#if the submission time has passed lock submission upon waking up
chmod ugo-w "$submissionFolder"


#Once the submission directory is locked, copy files to local directory or current directory
if [ -d $localDirectory ]; then
    cp -r "$submissionDirectory" "$localDirectory"
else
    cp -r "$submissionDirectory" pwd
fi
 
Old 10-19-2012, 08:45 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Always use "debug mode" when testing:
Code:
set -vxe
then try again for example running:
Code:
/bin/interpreter -vx /path/to/script {any input} 2>&1 | tee /path/to/logfile
Only when you can not see and fix the error post "/path/to/logfile", preferably in [code]vBB tags[/code].
 
  


Reply

Tags
date


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM


All times are GMT -5. The time now is 08:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration