cd script - Programming On Unix

Users browsing this thread: 1 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
venam
Administrators
Maybe those two shell commands: `pushd` and `popd` might be useful for what you intend to do:
https://en.wikipedia.org/wiki/Pushd_and_popd
darthlukan
Members
Remove `exec bash` from the script and set an alias for your script that looks like: `alias uni=". script_name.sh"`. Note the "." in the alias, "." is an alias for `source` which imports code to the shell, because you need this code in your current shell, not in a new shell, which is what you're creating with your current script.

Here's a gist that shows you the concept without any cruft: https://gist.github.com/darthlukan/0af01...le-mycd-sh

Reference this Stack Overflow answer: https://unix.stackexchange.com/a/156394
Github: https://github.com/darthlukan
CRUX Ports: http://ports.brianctomlinson.com
GPG: 3694569D
"We're all human, act accordingly." -- Me
r4ndom
Members
(03-05-2017, 07:27 AM)venam Wrote: Maybe those two shell commands: `pushd` and `popd` might be useful for what you intend to do:
https://en.wikipedia.org/wiki/Pushd_and_popd
That's awesome. I should really dive into /usr/bin and search for these kind of treasures. But for this problem I will go with darthlukan's approach (explanation below).

(03-05-2017, 12:19 PM)darthlukan Wrote: Remove `exec bash` from the script and set an alias for your script that looks like: `alias uni=". script_name.sh"`. Note the "." in the alias, "." is an alias for `source` which imports code to the shell, because you need this code in your current shell, not in a new shell, which is what you're creating with your current script.
Thanks. I prefer this a little more over venam's suggestion because it does not change the `pushd`, `popd` stack, which might be in use otherwise (currelty not, because I first need to get used to these commands).
venam
Administrators
(03-05-2017, 04:36 PM)r4ndom Wrote: That's awesome. I should really dive into /usr/bin and search for these kind of treasures. But for this problem I will go with darthlukan's approach (explanation below).
Technically, those are built-in inside the shells and not in /usr/bin.
So you should check the manual for your shell instead.
rocx
Members
(04-05-2017, 12:08 AM)venam Wrote: So you should check the manual for your shell instead.

The only shells they don't seem available in are the Korn shells (MirOS and regular). Otherwise it's in the C, Bourne Again, Z, and the Friendly Interactive shells.
z3bra
Grey Hair Nixers
Code:
cd -

Saves my life 10 times a day: it goes back to the last directory, and it works in all damn shells :)

For the `exec bash` thingy, it should not return afger you exit it. Your term application might respawn a shell automatically when its process ends.