Friday, January 18, 2013

AppleScript to open any web interface/account like GMail, etc

Few years back I switched to Mac and since then I'd say it's been great. Yes, Macs are expensive but they do offer great things in comparison. Anyway, since I'm not doing a sales pitch here I'll go ahead and say how you can automate logging onto any of your web accounts, for example your gmail account. The reason I do this is that I open Google web interface for both email and calendar everyday early in the morning at my work. Eventually, you'd think of doing it with just one click and so I came up with this.

First, open AppleScript editor on your mac and type what I show here below:
tell application "Safari" to open location "https://mail.google.com"
delay 5
tell application "Safari" to activate
tell application "System Events"
 keystroke "username"
 keystroke tab
 keystroke "password"
 keystroke return
end tell
Most of the times usernames and passwords are contain numerals. Entering keystroke "Sreedhar123" for username would just enter "Sreedhar" in the username field without 123 at the end of it. Which I'd say is a problemo. To avoid this from happening we need to enter applescript key codes for the numbers. So, for "Sreedhar123" you need to put this code:
tell application "Safari" to open location "https://mail.google.com"
delay 5
tell application "Safari" to activate
tell application "System Events"
 keystroke "Sreedhar"
        key code 18
        key code 19
        key code 20
 keystroke tab
 keystroke "password"
 keystroke return
end tell
Remember that same goes for password as well. Google "applescript keycodes" if you are looking a keycode for a specific character.


Of course, you can test this script immediately without having to save it by clicking on "Run" in the AppleScript menu. Once your'e satisfied save it as an application by choosing "Application" from the dropdown menu for File Format. You can drag the saved application onto your dock. Clicking it would take you to gmail web interface with just one and only one click. Nice! Right?

Now, remember that once you save it as an app you wouldn't be able to change the code anymore. If you'd like to see or change the code you'll have to save it in Script format by choosing "Script" for File Format which is default. Again, it's a no brainer doing something like this because you've your password exposed. Whereas saving it as an app doesn't expose anything.

Doing this way you can automate logging onto any web interface with just one click there by avoiding typing the web page address, entering the username, password and then entering return.

Finally, as you see I put "delay 5" in the code so that it waits for 5 seconds before trying to enter the username. The reason is that sometimes a page takes 2 to 3 seconds to load. If it takes more than 5 seconds then change this number. Otherwise, nothing happens as applescript goes through the code executing each line. Which means it'll have entered the username and password into something as the page hasn't loaded yet. In the worst case this might end up typing full or partial username and password into the address bar. So, be careful.

Wednesday, January 16, 2013

Making an app with an applescript on your mac that gets you public transit directions


At the end of the day in my office, just before leaving the office I generally check google maps for getting public transit directions and time tables. Google Maps lets you save driving directions with a link "Save to My Maps" under the directions to your "My places". But this is not the case with public transit directions. Which in my opinion sucks. This might sound trivial to some one who uses their car to goto office. Trust me it's a heck of nuisance to someone like me as I use public transit all the time.

Generally, what I do is I open a new tab in my browser and then open the page "maps.google.com" and then click on public transit, then type the addresses, let's say for example home or office if you've saved them or type the entire addresses and then select the train or bus from options based on your public transit mode. Again, it doesn't sound that big of a deal if you have to do this once in a month. But I do this million times in a month. So, I've started looking for some sort of a way to save it to my places.

Whatever I found googling didn't help me at all. In the end I've decided to make an app for my self with an applescript on my mac. Then I thought of putting it here if someone is interested in knowing how I did it. Here we go:

First goto maps.google.com and get the public transit directions for your destination. Let's say we need public transit directions from Hoboken HBLR Terminal to Penn Station, New York, NY. I know that there is a PATH train service that runs from Hoboken to 34th street which is where Penn station. Of course, you don't need to know anything of this at all. As I know I always select the option "train" from show options. Then I get the link to directions from share symbol next to My places. This is what I've got here:

https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0



Once you have this link, go and open applescript editor (Press command + space bar and type applescript) and add this code below and then save it to where ever you want to have it in your system. I put it on my desktop.

Applescript Code:

do shell script "open -a Safari \"https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0\""


The above code opens the tab with public transit directions in Safari.

To open the page in a default browser, just remove -a Safari:

do shell script "open \"https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0\""


Before making it an app you can simply try running it with "Run" on the editor which shows you whether it is working or not. Once you know that it is working the way you wanted it to be go for save options and there select "Application" in the "File Format" drop down menu. Once you save it as application, let's say on your Desktop you can simply drag it onto your Dock.

Once you have it there on the dock you can simply click it just like you open any other application. This opens public transit directions with your selected public transportation mode with the latest time table.

By the way, if you'd like to save your script so that you can modify or refer to in the future then I say save another copy some where in SCPT format by choosing SCPT from File Format drop down menu options.

Saturday, August 27, 2011

MySQL with ODBC driver

Configuring MySQL and ODBC driver

# more /etc/odbcinst.ini
# Example driver definitinions
#
#
# Included in the unixODBC package
[PostgreSQL]
Description    = ODBC for PostgreSQL
Driver        = /usr/lib64/libodbcpsql.so
Setup        = /usr/lib64/libodbcpsqlS.so
FileUsage    = 1
# Driver from the MyODBC package
# Setup from the unixODBC package
[MySQL]
Description    = ODBC for MySQL
Driver        = /usr/lib64/libmyodbc3.so
Setup        = /usr/lib64/libodbcmyS.so
FileUsage    = 1

/etc/odbc.ini

# more /etc/odbc.ini
[ODBC]
Description = Moab MySQL Database
Driver = MySQL
USER = root
PASSWORD = <write sql root password here>
Server = localhost
Database = Moab
Port = 3306
Socket = /var/lib/mysql/mysql.sock
Option =
Stmt =
The above standard odbc driver file syntax should be used to configure how your application talks to MySQL. Which means the file will have the same content as /etc/odbc.ini. For example application moab uses the file dsninfo.dsn in /opt/moab directory.
# more /opt/moab/dsninfo.dsn
[ODBC]
Driver = MySQL
USER = root
PASSWORD = <write sql password here>
Server = localhost
Database = Moab
Port = 3306
Socket = /var/lib/mysql/mysql.sock
Option =
Stmt =

Testing

Test the ODBC to MySQL connection by running the isql command, which reads the /etc/odbc.ini file:
# isql -v ODBC
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
Segmentation fault

Solution

To get rid of the error "segmentation fault" we need to recompile and install the unixODBC with an extra configure options.

Installing unixODBC

Web link: http://www.unixodbc.org/
# wget ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.0.tar.gz
# tar xzvf unixODBC-2.3.0.tar.gz
# cd unixODBC-2.3.0
# ./configure --enable-stats=no --enable-gui=no --sysconfdir=/etc --prefix=/usr
# make
# make install
--enable-stats=no is an attempt to correct the segmentation fault.
--enable-gui=no provides no GUIs, is used when ./configure can't find the X includes.

Installing ODBC Driver: MyODBC

Web link: http://www.mysql.com/products/connector/
Web link: http://www.mysql.com/downloads/connector/odbc/
# rpm -ivh --nodeps MyODBC-3.51.07-1.i586.rpm
--nodeps avoids aborting because rpm can't find libmyodbc that does exist in /usr/lib.
When isql won't work because it can't find the library, go make a link in /usr/lib to libmyodbc3-3.51.07.so and name it libmyodbc.so.

Testing

Test the ODBC to MySQL connection by running the isql command, which reads the /etc/odbc.ini file:
# isql -v ODBC
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
sreeSQL> show tables
+-----------------------------------------------------------------+
| Tables_in_Moab                                                  |
+-----------------------------------------------------------------+
| EventType                                                       |
| Events                                                          |
| GeneralStats                                                    |
| GenericMetrics                                                  |
| Jobs                                                            |
| Moab                                                            |
| NodeStats                                                       |
| NodeStatsGenericResources                                       |
| Nodes                                                           |
| ObjectType                                                      |
| Requests                                                        |
| mcheckpoint                                                     |
+-----------------------------------------------------------------+
SQLRowCount returns -4294967284
12 rows fetched
SQL> quit
[root@cuda unixODBC-2.3.0]#

Thursday, August 25, 2011

Applescript for Apple Mail

Almost 6 months back I encountered a problem with desktop mail client on Mac. I had to use Applescript to solve the problem. I always wanted to copy whatever email I get onto my mac. I setup rules to copy certain emails based on specific criteria on my mac. The problem was whenever I read an email it was still unread on my mac. I tried every other method to make their status read. Nothing worked and so finally I opted for Applescript. You can see that the number of unread mail count on my mac is 716 while inbox has zero.

I wrote this applescript to solve this problem. Here it is:
tell application "Mail"
	set theMailboxes to every mailbox
	repeat with theMailbox in theMailboxes
		set theMessages to every message of theMailbox
		repeat with theMessage in theMessages
			if read status of theMessage is false then
				set read status of theMessage to true
			end if
		end repeat
	end repeat
end tell


It doesn't change the status of new emails in your inbox. It only changes the status of emails on your mac. Once you run the script in applescript editor you can see the count dropping down to zero very quickly.

The good thing with the script is you can make this run at specific times in a day by using ical. Just use alarm options in iCal to setup to run this script at certain of the day. I run this script at 8am early in the morning and at noon and at 4 pm, etc so that I don't see the annoying email count on my mac.

Wednesday, August 24, 2011

Locking screen on Mac from Terminal

I do all my work from a terminal on my mac. I try to use as much keyboard as possible for everything. Whenever I need to lock my screen which I say many times in a day I either use hot corners or key chain options. Then I thought why not from terminal it self. In case you are also interested to know how to do this, here is how I did it.

Open up a terminal and do these commands. The last command "schlock" will lock the screen whenever you execute it from the terminal. Of course, you can give what ever name you like. Make sure that it doesn't have space between the words, for example "screen lock". You can always give something like "screen_lock", etc.

$ echo "alias scrlock='/System/Library/CoreServices/\"Menu Extras\"/\
User.menu/Contents/Resources/CGSession -suspend'" >> ~/.bashrc
$ source ~/.bashrc
$ scrlock

Tuesday, August 23, 2011

Installing XmGrace (Grace) on Mac OS X

We need to install motif libraries first. You can download binary from here:
http://www.ist.co.uk/downloads/motif_download.html

Grace Download: http://plasma-gate.weizmann.ac.il/Grace/

$ cd Desktop
$ wget ftp://ftp.fu-berlin.de/unix/graphics/grace/src/grace5/grace-latest.tar.gz
$ tar xzvf grace-latest.tar.gz
$ cd grace-5.1.22
$ ./configure --x-includes=/usr/X11/include --x-libraries=/usr/X11/lib \
--with-motif-library=-lXm  --with-extra-incpath=/usr/OpenMotif/include:/Users/sm4082/fftw-2.1.5/include \
--with-extra-ldpath=/usr/OpenMotif/lib:/Users/sm4082/fftw-2.1.5/lib
$ make
$ sudo make install
$ echo "export PATH=$PATH:/usr/local/grace/bin" >> ~/.bashrc
$ source ~/.bashrc
$ xmgrace

Now you should have xmgrace at your prompt. Cool. Right?

Monday, August 22, 2011

Torque qsub wrapper/submit filter shell script

I have recently written this shell script to act as a filter to pbs job scripts submitted on our clusters. For more information look online for Torque administrator manual.
#!/bin/bash

##########################################################################################################
# This acts as a wrapper/filter to qsub. Written by Sreedhar Manchu.
# Added on Aug 8th, 2011
# http://www.clusterresources.com/torquedocs21/a.jqsubwrapper.shtml
##########################################################################################################

# Memory Variables.
# If you want to provide more memory for jobs than the currently allocated memory on
# nodes on certain chassis you just need to change these variables.

mem16gb_limit=14		 # Right now 14GB memory is allocated for jobs on 16GB nodes
mem32gb_limit=30		 # Right now 30GB memory is allocated for jobs on 32GB nodes
mem_bigmem_min=18        # Minimum memory required for bigmem queue in GB
memory_per_core=1536	 # Memory allocated per core is 1536MB (1.5GB). If you are changing this number make sure 
                         # that the processors per node times this number is at least 1GB less than physcal memory
						 # present on the node. In this case, it is 16GB. 8*1536MB=12GB < 15GB. This number 
                         # should be in MB.

wtime_bigmem_max=172800  # Bigmem queue maximum walltime in seconds (48 hours)
                         # Should be a multiple of 3600.
wtime_p12_max=43200      # p12 queue maximum walltime in seconds (12 hours)
                         # Should be a multiple of 3600.
wtime_p48_max=172800     # p48 queue maximum walltime in seconds (48 hours)
                         # Should be a multiple of 3600.
wtime_serlong_min=172860 # serlong queue minimum walltime in seconds (48 hours 1 minute)
					     # The remainder after deviding it with 3600 should be a multiple of 60.
wtime_serlong_max=345600 # serlong queue maximum walltime in seconds (96 hours)
                         # Should be a multiple of 3600.
wtime_ser2_min=60		 # ser2 queue minimum walltime in seconds (1 minute).
                         # Should be a multiple of 60. For example, it can be 120 (=2*60)
wtime_ser2_max=172800    # ser2 queue maximum walltime in seconds (48 hours)
                         # Should be a multiple of 3600.

# Don't change the code in the block bordered by dashes

#---------------------------------------------------------------------------------------------------------

if [ $# -eq 1 ]   # Enters loop when job runs through only script. Hence argument is 1.
then              # This part of the code makes sure that script follows the pbs scripting rules.
	counter=0
	line_count=0
	while read -r i
	do
   		if [ $line_count -eq 0 ]
   		then
      		if echo $i | egrep "^#\! */" 2>&1 > /dev/null  # Looks for shebang/hashbang (#!) in the first line
      		then
         		echo $i
      		else
         		echo -e "\nFirst line should have shebang/hashbang (#!). Please fix the script\n" > /dev/tty
				echo -e "For example, it can be #!/bin/sh or #!/bin/bash , etc.\n" > /dev/tty
         		echo -e "For more information please refer to https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		exit -1
      		fi
      		line_count=1
      		pbs_operatives="not_done"
      		pbs_line_count=0
      		pbs_first_line="yes"
      		continue
   		elif [[ $pbs_operatives == "not_done" ]]
   		then
      		if [ $pbs_first_line = "yes" -a `echo $i | egrep "^$" 2>&1 > /dev/null` $? -ne 0 -a `echo $i | egrep "^ *#" | grep -v "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 -a `echo $i | egrep "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 ]
      		then
         		echo -e "\nPBS operatives such as \"#PBS -l nodes=1:ppn=2,walltime=05:00:00\" are either missing or not declared immediately after shebang/hashbang line \"#\!/bin/bash\". Please fix the script.\n" > /dev/tty
         		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		exit -1
      		else
         		pbs_first_line="no"
         		if [ `echo $i | egrep "^$" 2>&1 > /dev/null` $? -eq 0 -o `echo $i | egrep "^ *#" | grep -v "^ *#PBS *" 2>&1 > /dev/null` $? -eq 0 ]
         		then
            		echo $i
            		line_count=$(($line_count+1))
            		if [ `head -$(($line_count+1)) $1 | tail -1 | egrep "^$" 2>&1 > /dev/null` $? -ne 0 -a `head -$(($line_count+1)) $1 | tail -1 | egrep "^ *#" | grep -v "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 -a `head -$(($line_count+1)) $1 | tail -1 | egrep "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 ]  
            		then
               			if [ $pbs_line_count -eq 0 ]
               			then
                  			echo -e "\nPBS operatives such as \"#PBS -l nodes=1:ppn=2,walltime=05:00:00\" are either missing or not declared immediately after shebang/hashbang line \"#\!/bin/bash\". Please fix the script.\n" > /dev/tty
                  			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			else
                  			pbs_operatives="done"
               			fi
            		fi
            		continue    # This part of the code finds the required parts from #pbs lines
         		elif echo $i | egrep "^ *#PBS  *" 2>&1> /dev/null    # Looks for #PBS lines
         		then
      	   			if echo $i | egrep "^ *#PBS  *-q  *" 2>&1> /dev/null   # Looks for #PBS -q line
            		then
               			queue=$i    # Stores the queue requested
               			echo $i
            		elif echo $i | egrep "^ *#PBS  *-l  *" 2>&1> /dev/null   # Looks for #PBS -l lines
            		then
               			if echo $i | egrep " *nodes=0*[1-9][0-9]?" 2>&1> /dev/null  # Looks for nodes= in #PBS -l line
               			then
                  			node_count=`echo $i | egrep -o 'nodes=0*[1-9][0-9]?' | cut -d"=" -f2`   # stores number of nodes requested
                  			#echo "node count is $node_count"
               			fi
               			if echo $i | egrep "ppn=0*[1-9][0-9]?" 2>&1> /dev/null   # Looks for ppn= in #PBS -l line
               			then
                  			ppn_count=`echo $i | egrep -o 'ppn=0*[1-9][0-9]?' | cut -d"=" -f2`   # stores number of ppn requested
                  			#echo "ppn count is $ppn_count"
               			fi
               			if echo $i | egrep " *mem=0*[1-9][0-9]*[a-zA-Z][a-zA-Z]" 2>&1> /dev/null   # looks for mem= in #PBS -l line
               			then
							if echo $i | egrep -w " *mem=0*[1-9][0-9]*[a-zA-Z][a-zA-Z]" 2>&1> /dev/null
							then
                  				mtag=`echo $i | egrep -o ' *mem=0*[1-9][0-9]*[a-zA-Z][a-zA-Z]' | cut -d= -f2 | egrep -o '[a-zA-Z][a-zA-Z]'`   # stores memory tag like KB, MB or GB
                  				memory=`echo $i | egrep -o 'mem=0*[1-9][0-9]*' | cut -d"=" -f2`
                  				if echo $mtag | egrep '[gG][bB]' 2>&1> /dev/null
                  				#if [[ $mtag =~ "[gG][bB]" ]]
                  				then
                     				memory=`expr $memory '*' 1024 '*' 1024`   # stores memory requested and converts it into kilo bytes
                     				#memory=$(($memory*1024*1024))
                  				elif echo $mtag | egrep '[mM][bB]' 2>&1> /dev/null
                  				#if [[ $mtag =~ "[mM][bB]" ]]
                  				then
                     				memory=`expr $memory '*' 1024`
                     				#memory=$(($memory*1024))
                  				elif echo $mtag | egrep '[kK][bB]' 2>&1> /dev/null
                  				#if [[ $mtag =~ "[kK][bB]" ]]
                  				then
                     				memory=$memory
								else
									echo -e "\nMemory should be either in Kilo Bytes(kb, KB) or Mega Bytes (mb, MB) or Giga Bytes (gb, GB). Please fix the script.\n" > /dev/tty
									echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
									exit -1
								fi
							else
								echo -e "\nMemory tag should not be more than two letters. It can be just one of kb,KB,mb,MB,gb and GB. Please fix the script.\n" > /dev/tty
								echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
								exit -1
                  			fi
               			fi
               			if echo $i | egrep -o '( |,)walltime=[0-9]*:?[0-9]*:?[0-9]*:?[0-9]*($|,)' 2>&1> /dev/null   # looks for walltime= in #PBS -l line
               			then
                  			wallstring=`echo $i | egrep -o '( |,)walltime=[0-9]*:?[0-9]*:?[0-9]*:?[0-9]*($|,)' | cut -f2 -d"="`
                  			if [ `echo "$wallstring" | egrep -o '^[0-9]*:?[0-9]*:?[0-9]*:?[0-9]*,$'` ]
                  			then
                     			wallstring=`echo "$wallstring" | cut -f1 -d","`
                  			fi
                  			if [ -z "$wallstring" ]
                  			then
                     			echo -e "\nPlease declare the walltime in the script such as in \"#PBS -l walltime=05:00:00\" or \"#PBS -l nodes=1:ppn=8,walltime=05:00:00\"\n" > /dev/tty
                     			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                     			exit -1;
                  			elif [ `echo "$wallstring" | egrep -o '^:{1,3}$'` ]
                  			then
                     			echo -e "\nPlease declare the walltime in the script such as in \"#PBS -l walltime=05:00:00\" or \"#PBS -l nodes=1:ppn=8,walltime=05:00:00\"\n" > /dev/tty
                     			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                     			exit -1;
                  			else
                     			number=`echo "$wallstring" | tr -dc ":" | wc -c`
                     			for  (( j=$(($number+1)); j>0; j-- ))
                     			do
                        			if [ -n "`echo "$wallstring" | cut -f"$j" -d":"`" -a $j -eq $(($number+1)) ]
                        			then
                                		walltime=`echo "$wallstring" | cut -f"$j" -d":"`    # stores walltime requested
                        			elif [ -n "`echo "$wallstring" | cut -f$j -d":"`" -a $j -eq $number ]
                        			then
                                		walltime=$(($walltime+`echo "$wallstring" | cut -f$j -d":"`*60))   # converts into seconds
                        			elif [ -n "`echo "$wallstring" | cut -f$j -d":"`" -a $j -eq $(($number-1)) ]
                        			then
                                		walltime=$(($walltime+`echo "$wallstring" | cut -f$j -d":"`*60*60))   # converts into seconds
                        			elif [ -n "`echo "$wallstring" | cut -f$j -d":"`" -a $j -eq $(($number-2)) ]
                        			then
                                		walltime=$(($walltime+`echo "$wallstring" | cut -f$j -d":"`*24*60*60))  # converts into seconds
                        			fi
                     			done
                  			fi
                  			#echo "walltime is $walltime"
               			fi
               			echo $i
            		else
               			echo $i
            		fi
            		line_count=$(($line_count+1))
            		pbs_line_count=$(($pbs_line_count+1))
            		if [ `head -$(($line_count+1)) $1 | tail -1 | egrep "^$" 2>&1 > /dev/null` $? -ne 0 -a `head -$(($line_count+1)) $1 | tail -1 | egrep "^ *#" | grep -v "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 -a `head -$(($line_count+1)) $1 | tail -1 | egrep "^ *#PBS *" 2>&1 > /dev/null` $? -ne 0 ]  
            		then
               			pbs_operatives="done"
            		fi
            		continue
         		fi 
 			fi     
		fi
   
   		if [ -z $node_count ]
   		then
      		echo -e "\nNode count is not declared such as in \"#PBS -l nodes=1:ppn=8,walltime=05:00:00\". Please fix the script.\n" > /dev/tty
      		echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
      		exit -1
   		fi
   		if [ -z $ppn_count ]
   		then
      		echo -e "\nProcessor Per Node count is not declared such as in \"#PBS -l nodes=1:ppn=8,walltime=05:00:00\". Please fix the script.\n" > /dev/tty
      		echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
      		exit -1
   		else
      		if [ $ppn_count -gt 8 ]
      		then
         		echo -e "\nMaximum number of processors per node available is 8. Please fix the script.\n" > /dev/tty
         		echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		exit -1
      		fi
   		fi
   		if [ -z $walltime ]
   		then
      		echo -e "\nPlease declare the walltime in the script such as in \"#PBS -l walltime=05:00:00\" or \"#PBS -l nodes=1:ppn=8,walltime=05:00:00\"\n" > /dev/tty
      		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
      		exit -1;
   		fi

#---------------------------------------------------------------------------------------------------------

# This part of the code works based on the queues declared.

   		if [ -n "$queue" -a $counter -eq 0 ]
   		then
      		if echo $queue | egrep "^ *#PBS  *-q  *bigmem$" 2>&1> /dev/null   # Enters if the queue is bigmem
      		then
         		if [ -z "$memory" ]  # If memory is not requested it echoes the statement below.
         		then
            		echo -e "\nBigmem queue needs memory declaration in the pbs script. For example, if your job needs 25GB memory, job script should contain a line:" > /dev/tty
            		echo -e "\n#PBS -l mem=25GB\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		exit -1
         		else
               		if [ $memory -lt $(($mem_bigmem_min*1024*1024)) ]
               		then
		  				echo -e "\nMinimum memory for bigmem queue is ${mem_bigmem_min}GB. Please fix the script.\n" > /dev/tty
   		  				echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
                  		exit -1
               		elif [ $memory -gt $(($mem32gb_limit*1024*1024)) ]
               		then
                  		echo -e "\nMaximum memory for bigmem queue is ${mem32gb_limit}GB. Please fix the script.\n" > /dev/tty
                  		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
                  		exit -1
               		fi
         		fi
         		if [ $walltime -gt $wtime_bigmem_max ]   # makes sure walltime is less than 48 hours
         		then
            		echo -e "\nThe maximum walltime for bigmem jobs is $(($wtime_bigmem_max/3600)) hours. Please fix the walltime before you resubmit the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		fi
      		elif echo $queue | egrep "^ *#PBS  *-q  *p12$" 2>&1> /dev/null   # looks for p12 queue in #PBS -q line
      		then
         		if [ ! -z $node_count -a $node_count -eq 1 ]   # p12 jobs require minimum 2 nodes
         		then
               		echo -e "\nMinimum number of nodes needed for queue p12 is 2. Please fix your job script.\n" > /dev/tty
               		echo  -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
               		exit -1
         		elif [ -n "$memory" -a -n $node_count ]
         		then
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nTo run parallel jobs that require memory greater than ${mem16gb_limit}GB please use queue bigmem.\n" > /dev/tty
               			echo  -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
               			exit -1
            		fi
         		elif [ -z "$memory" ]
         		then
            		echo "#PBS -l mem=${mem16gb_limit}GB"
            		echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n"> /dev/tty
               		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		fi
         		if [ $walltime -gt $wtime_p12_max ]  # makes sure walltime is less than 12 hours for p12 jobs
         		then
            		echo -e "\nThe maximum walltime for p12 jobs is $(($wtime_p12_max/3600)) hours. Please fix the walltime before you resubmit the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		fi
      		elif echo $queue | egrep "^ *#PBS  *-q  *p48$" 2>&1> /dev/null   # p48 queue
      		then
         		if [ ! -z $node_count -a $node_count -eq 1 ]
         		then
            		echo -e "\nMinimum number of nodes needed for queue p48 is 2. Please fix your job script.\n" > /dev/tty
            		echo  -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		exit -1
         		elif [ -n "$memory" -a -n $node_count ]
         		then
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nTo run parallel jobs that require memory greater than ${mem16gb_limit}GB please use queue bigmem.\n" > /dev/tty
               			echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
               			exit -1
            		fi
         		elif [ -z "$memory" ]
         		then
            		echo "#PBS -l mem=${mem16gb_limit}GB"
            		echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n"> /dev/tty
               		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		fi
         		if [ $walltime -gt $wtime_p48_max ]
         		then
            		echo -e "\nThe maximum walltime for p48 jobs is $(($wtime_p48_max/3600)) hours. Please fix the walltime before you resubmit the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		fi
      		elif echo $queue | egrep "^ *#PBS  *-q  *serlong$" 2>&1> /dev/null   # serlong queue
      		then
         		if [ ! -z $node_count -a $node_count -gt 1 ]
         		then
            		echo -e "\nThe maximum node count for serlong queue is 1. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		elif [ $walltime -lt $wtime_serlong_min ]
         		then
            		echo -e "\nThe minimum walltime for serlong queue is $(($wtime_serlong_min/3600)) hours $((($wtime_serlong_min-3600*($wtime_serlong_min/3600))/60)) minute. Please fix the script.\n" > /dev/tty  
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		elif [ $walltime -gt $wtime_serlong_max ]
         		then
            		echo -e "\nThe maximum walltime for serlong queue is $(($wtime_serlong_max/3600)) hours. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		fi
         		if [ -n "$memory" ]
         		then
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nMaximum memory available for serlong queue is ${mem16gb_limit}GB. To request more than this please use queue bigmem.\n" > /dev/tty
               			echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
               			exit -1
            		fi
            		if [ $ppn_count -lt 8 ]
            		then
               			if [ $(($ppn_count*$mem16gb_limit*1024*1024/8)) -lt $memory ]
               			then
                  			echo -e "\nAllocated memory for each core is $(($mem16gb_limit*1024/8))MB (=${mem16gb_limit}GB/8). Please change the ppn count in the script according to the calculation, ppn*$(($mem16gb_limit*1024/8))MB >= declared memory for the job.\n" > /dev/tty
                  			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			fi
            		else
               			if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
               			then
                  			echo -e "\nMaximum memory available is ${mem16gb_limit}GB. Please fix the script.\n" > /dev/tty
                  			echo -e "For more information refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			fi
            		fi
         		else
            		if [ $ppn_count -lt 8 ]
            		then
               			echo "#PBS -l mem=$(($ppn_count*$memory_per_core))MB"
               			echo -e "\nAllocated memory for your job is $(($ppn_count*$memory_per_core))MB. Memory is calculated using the formula ppn times memory per core, ${memory_per_core}MB." > /dev/tty
               			echo -e "If you think you need more memory than this please either change ppn count or declare memory with #PBS -l mem=xxGB\n" > /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		else
               			echo "#PBS -l mem=${mem16gb_limit}GB"
               			echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n" > /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		fi
         		fi
      		elif echo $queue | egrep "^ *#PBS  *-q  *ser2$" 2>&1> /dev/null   # ser2 queue
      		then
         		if [ ! -z $node_count -a $node_count -gt 1 ]
         		then
            		echo -e "\nThe maximum node count for ser2 queue is 1. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		elif [ $walltime -lt $wtime_ser2_min ]
         		then
            		echo -e "\nThe minimum walltime for ser2 queue is $(($wtime_ser2_min/60)) minute. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		elif [ $walltime -gt $wtime_ser2_max ]
         		then
            		echo -e "\nThe maximum walltime for ser2 queue is $(($wtime_ser2_max/3600)) hours. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		fi
         		if [ -n "$memory" ]
         		then
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nMaximum memory available for ser2 queue is ${mem16gb_limit}GB. To request more than this please use queue bigmem.\n" > /dev/tty
               			echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
               			exit -1            
            		fi
            		if [ $ppn_count -lt 8 ]
            		then
               			if [ $(($ppn_count*$mem16gb_limit*1024*1024/8)) -lt $memory ]
               			then
                  			echo -e "\nAllocated memory for each core is $(($mem16gb_limit*1024/8))MB (=${mem16gb_limit}GB/8). Please change the ppn count in the script according to the calculation, ppn*$(($mem16gb_limit*1024/8))MB >= declared memory for the job.\n" > /dev/tty
                  			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			fi
            		else
               			if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
               			then
                  			echo -e "\nMaximum memory available is ${mem16gb_limit}GB. Please fix the script.\n" > /dev/tty
                  			echo -e "For more information refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			fi
            		fi
         		else
            		if [ $ppn_count -lt 8 ]
            		then
               			echo "#PBS -l mem=$(($ppn_count*$memory_per_core))MB"
               			echo -e "\nAllocated memory for your job is $(($ppn_count*$memory_per_core))MB. Memory is calculated using the formula ppn times memory per core,${memory_per_core} MB." > /dev/tty
               			echo -e "If you think you need more memory than this please either change ppn count or declare memory with #PBS -l mem=xxGB\n" > /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		else
               			echo "#PBS -l mem=${mem16gb_limit}GB"
               			echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n"> /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		fi
         		fi
      		fi
      		counter=1

# This part of the code works when there are no queues declared.

   		elif [ -z "$queue" -a $counter -eq 0 ]
   		then
      		if [ -n $node_count -a $node_count -eq 1 ]
      		then
         		if [ $walltime -lt $wtime_ser2_min ]
         		then
            		echo -e "\nThe minimum walltime for serial jobs is $(($wtime_ser2_min/60)) minute. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
         		elif [ $walltime -gt $wtime_serlong_max ]
         		then
	    			echo -e "\nThe maximum walltime for serial jobs is $(($wtime_serlong_max/3600)) hours. Please fix the script.\n" > /dev/tty
	    			echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
	 			fi
         		if [ -z "$memory" ]
         		then
            		if [ -n $ppn_count -a $ppn_count -lt 8 ]
            		then
               			echo "#PBS -l mem=$(($ppn_count*$memory_per_core))MB"
               			echo -e "\nAllocated memory for your job is $(($ppn_count*$memory_per_core))MB. Memory is calculated using the formula ppn times memory per core, ${memory_per_core}MB." > /dev/tty
               			echo -e "If you think you need more memory than this please either change ppn count or declare memory with #PBS -l mem=xxGB\n" > /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		elif [ -n $ppn_count -a $ppn_count -eq 8 ]
            		then
               			echo "#PBS -l mem=${mem16gb_limit}GB"
               			echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n"> /dev/tty
               			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
            		fi
         		else
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nMaximum memory available is ${mem16gb_limit}GB. To request more than this please use queue bigmem.\n" > /dev/tty
               			echo -e "For more information please refter to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
               			exit -1            
            		fi
            		if [ $ppn_count -lt 8 ]
            		then
               			if [ $(($ppn_count*$mem16gb_limit*1024*1024/8)) -lt $memory ]
               			then
                  			echo -e "\nAllocated memory for each core is $(($mem16gb_limit*1024/8))MB (=${mem16gb_limit}GB/8). Please change the ppn count in the script according to the calculation, ppn*$(($mem16gb_limit*1024/8))MB >= declared memory for the job.\n" > /dev/tty
                  			echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
                  			exit -1
               			fi
            		fi
         		fi
      		elif [ -n $node_count -a $node_count -ge 2 ]
      		then
	 			if [ $walltime -gt  $wtime_p48_max ]
	 			then
	    			echo -e "\nMaximum walltime for parallel jobs is $(($wtime_p48_max/3600)) hours. Please fix the script.\n" > /dev/tty
            		echo -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Queues\n" > /dev/tty
            		exit -1
	 			fi
         		if [ -n "$memory" ]
         		then
            		if [ $memory -gt $(($mem16gb_limit*1024*1024)) ]
            		then
               			echo -e "\nTo run parallel jobs that require memory greater than ${mem16gb_limit}GB please use queue bigmem.\n" > /dev/tty
               			echo  -e "For more information please refer to HPC wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
               			exit -1
            		fi
         		else
            		echo "#PBS -l mem=${mem16gb_limit}GB"
	    			echo -e "\nAllocated memory for your job is ${mem16gb_limit}GB. If you need more than this please use bigmem queue.\n"> /dev/tty
               		echo -e "For more information please refer to HPC Wiki at https://wikis.nyu.edu/display/NYUHPC/Running+jobs\n" > /dev/tty
         		fi
      		fi
      		counter=1
   		fi
   		echo $i
	done
# This below part works when job is submitted through command line but
# not through script. Where as the above part entirely deals with the pbs scripts.
else
	while read -r i
	do
		echo $i
	done
fi

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore