cd script - Programming On Unix

Users browsing this thread: 2 Guest(s)
r4ndom
Members
Hey folks,

since university started two weeks back and lectures, exercises and papers are there to be read I change my directory to the current term around 10 times a day which breaks down to the following commands

Code:
uni /* an alias jumping to the university directory */
cd 17ss /* the current term */

Of course this could be done easier by putting the following in a sources file

Code:
alias uni="cd $UNI_DIR && cd 17ss"

But as time goes on the terms do as well so the alias must be changed in the future -> not as ugly as before, but still ugly

So what about a script that gets the current term based on the last modification time, i.e., `ls -t1 $UNI_DIR | head -n1`. Nice.

So I came up with the following:

Code:
#!/bin/bash

usage()
{
    cat <<EOF
Usage: $(basename $0) [-hp]
    -h: Showing this message
    -p: Printing the directory
EOF
    exit 1
}

DIR="$HOME/usr/docs/uni/msc_itsec"
CURTERM="$DIR/$(ls -t1 $DIR | head -n1)"

case $1 in
    -h) usage ;;
    -p) printf "%s\n" $CURTERM ;;
    *) cd $CURTERM && exec bash ;;
esac

Now I have a script which changes the directory for me, nice. Maybe it is useful for you as well. Just wanted to share :)

Cheers,
r4ndom

--------------------
EDIT:
Found a nasty bug.

Code:
exec bash

replaces the current shell with bash (no problem for me, since I'm using bash anyways). But if you `exit` you enter the former shell again. Why is this happening? This is not the way replacement works or am I missing something?
---------------------------

What a random Github page


Messages In This Thread
cd script - by r4ndom - 03-05-2017, 06:35 AM
RE: cd script - by venam - 03-05-2017, 07:27 AM
RE: cd script - by darthlukan - 03-05-2017, 12:19 PM
RE: cd script - by r4ndom - 03-05-2017, 04:36 PM
RE: cd script - by venam - 04-05-2017, 12:08 AM
RE: cd script - by rocx - 04-05-2017, 12:46 PM
RE: cd script - by z3bra - 04-05-2017, 07:19 PM