Difference between revisions of "Scripting"

From Computer Science
Jump to: navigation, search
(Bash)
Line 31: Line 31:
  
 
Note that to make this file easily executable, you will want to chmod to make it executable by yourself. If the file is named handin_check.sh you could do: <code>chmod u+x handin_check.sh</code>
 
Note that to make this file easily executable, you will want to chmod to make it executable by yourself. If the file is named handin_check.sh you could do: <code>chmod u+x handin_check.sh</code>
 +
 +
This could be improved by checking the result of make to confirm that the project was successfully compiled, and similarly for changing directory and submitting the assignment. If you plan to use bash, zsh, tcsh, or one of the other shell languages, it would be good to go through a tutorial to get familiar with the syntax of the language. They are a bit different than standard programming languages.
  
 
= Python =
 
= Python =

Revision as of 14:27, 8 March 2024

A few example scripts. Most people will use bash, tcsh, or zsh for their scripting language. But you could also use python, perl, javascript, C, or whatever language you want. The shell languages are normally less typing, so it is worth getting used to one of them.

Here is an example shell script in bash to hand in a C programming assignment using the handin submission system that some CS faculty use.

Bash

#!/bin/bash                                                                                                                                     

if [ $# -lt 1 ]; then
    echo "Usage: ./handin_check.sh assignment [course]"
    echo "  This program will compile the assignment, hand it in, and run hwcheck."
    echo "  If course is not given, then default is the course you are logged into."
    exit
fi

assignment="$1"

cd "~/$assignment"
make

if [ $# -gt 1 ]; then
    course=$2
    submit -- course "$course"
    submit --hwcheck "$course"
else
    submit
    submit --hwcheck
fi

Note that to make this file easily executable, you will want to chmod to make it executable by yourself. If the file is named handin_check.sh you could do: chmod u+x handin_check.sh

This could be improved by checking the result of make to confirm that the project was successfully compiled, and similarly for changing directory and submitting the assignment. If you plan to use bash, zsh, tcsh, or one of the other shell languages, it would be good to go through a tutorial to get familiar with the syntax of the language. They are a bit different than standard programming languages.

Python

Here is a python script to do the same thing. If the file were named handin_check.py, then you would do this to make it executable: chmod u+x handin_check.py

#!/bin/env python3                                                                                                                              

import sys, os, subprocess

if len(sys.argv) < 1:
    print( "Usage: ./handin_check.sh assignment [course]")
    print("  This program will compile the assignment, hand it in, and run hwcheck.")
    print("  If course is not given, then default is the course you are logged into.")
    sys.exit()

assignment = sys.argv[1]

os.chdir(f"~/{assignment}")

if len(sys.argv) > 1:
    course = sys.argv[2]
    subprocess.run(f'submit --course {course}', shell=True)
    subprocess.run(f'submit --hwcheck --course {course}', shell=True)
else:
    subprocess.run('submit', shell=True)
    subprocess.run('submit --hwcheck', shell=True)