Scripting

From Computer Science
Revision as of 14:25, 8 March 2024 by Jkinne (talk | contribs) (Created page with "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....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

= 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)