Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Shellscripting question

Name: Anonymous 2012-02-07 10:24

How could I make a shellscript which will echo "Welcome home, oniichan!" whenever I cd to my home directory?

Name: Anonymous 2012-02-07 10:39

perhaps I could make a shellscript like this:
#!/bin/bash
cd ~
echo "Welcome home, oniichan!"

and then just alias it to "home"

but can you even alias shellscripts?

Name: Anonymous 2012-02-07 10:40

>>2
You may alias anything.

Name: Anonymous 2012-02-07 10:58

>>2

you should call it hm for EXPERT UNIX READABILITY

Name: Anonymous 2012-02-07 11:02

>>2
although you'd need to be careful, since this would change the directory in the shell script, not the parent shell, so when the script exited, you'd be back at where you were in the parent shell. It's tricky.

Name: Anonymous 2012-02-07 11:07

>>5
2/10. Go back to /g.

Name: wat !.JeS8NKync 2012-02-07 11:11

wat

Name: Anonymous 2012-02-07 11:43

alias cd ~ = 'cd ~ && echo "what"'

Name: Anonymous 2012-02-07 16:28

>>8
alias cd ~ = 'cd ~ && echo "what"'
Am I just dumb, or isn't it a bad idea to alias something to itself? Wouldn't the output of that aliased command be ``cd ~ what cd ~ what cd ~ what cd ~ what cd ~ what''... etc

Reminds me of something similar:
(save as what.bat)
@echo off
:start
echo what
what.bat
goto start

also I am high as fuck

smoke weed everyday, it makes thinking more difficult

Name: Anonymous 2012-02-07 16:39

If using zsh:
function cd () {
        if ! test "$@"; then
                $HOME || return $?
        else
                $@ || return $?
        fi
        if test "$PWD" = "$HOME"; then
                echo 'Welcome home, oniichan!'
        fi
}

Name: Anonymous 2012-02-07 16:47

>>9
That should be start what.bat to create a new process/window. Without start, the batch file never resumes and goto start is never reached. A common bug in batch files is to forget start or call, so the original batch file never resumes. The default behavior without call is to pop the entire chain of batch files from the call stack. Why did Microsoft decide that you need call for batch files instead of having them behave like any other executable? Because changing the default behavior would break compatibility with QDOS. Even in the early 1980s Microsoft and Intel products were full of kludges for backwards compatibility.

Name: Anonymous 2012-02-08 7:18

>>10
What part of this does not work with bash?

Name: Anonymous 2012-02-08 7:22

>>12
$HOME || return $? and $@ || return $?.
In zsh, you can change directory like you'd run an executable, the cd isn't needed.

Name: Anonymous 2012-02-08 7:25

>>13
You should've used chdir $HOME instead (also doesn't work in bash), otherwise you could do stupid stuff like cd /bin/ls.

Name: Anonymous 2012-02-08 7:28

>>13
bash version:
cd () {
    if ! test "$@"; then
        pushd $HOME > /dev/null || return $?
    else
        pushd $@ > /dev/null || return $?
    fi 
    if test "$PWD" = "$HOME"; then
        echo 'Welcome home!'
    fi 
}

Name: Anonymous 2012-02-08 7:33

>>1
You can start with something like
export PS1='$(if [ $PWD == $HOME ]; then echo "Welcome home, oniichan! "; fi)\$ '
It works in bash and changes your prompt every time you are in your home directory.

As for echoing a welcome message only when $PWD changes to your $HOME, I don't know, for one thing there's a shitload of things that can change your $PWD, also it would be all kinds of annoying if it fucked up scripts accidentally.

Name: Anonymous 2012-02-08 7:36

~/bin/cd:
#!/bin/sh
cd "$@"
if [ $PWD = $HOME ]
then
    echo "Welcome home, weeaboo!"
fi


.bashrc, .zshrc, etc.:
alias cd=~/bin/cd

Name: Anonymous 2012-02-08 7:37

>>17
You're only changing the directory in the new sh process.

Name: Anonymous 2012-02-08 7:39

>>18
alias cd='exec ~/bin/cd'

Name: >>16 2012-02-08 7:40

Wait, I actually have an idea.

You can use persistent state in your PS1 "script". Apparently, it's impossible to store it in environment variables, but you can always store it in a temp file. So you can see if $PWD has changed between two invocations of PS1. And you can output \n or two after the welcome message, to make it appear separate from the prompt.

Name: Anonymous 2012-02-08 7:45

>>19
That'll just close your shell.
Better also put exec $SHELL on the bottom of your ~/bin/cd.

Note that is horribly inefficient, makes your shell restart every time and parse its configuration files (and print any fortunes or whatever) every time you cd. >>13 is a way better solution.

Name: Anonymous 2012-02-08 7:45

>>21
Meant >>15, not >>13.

Name: Anonymous 2012-02-08 7:46

>>17 that would suck because 1) there are other ways to change current directory, like pushd/popd, 2) you don't want scripts that might change a directory to pollute their stdout with bullshit (I guess you can check if stdout is a tty).

But on the other hand, as I understand a buggy incomplete solution that does one thing only sometimes and does it badly is exactly what UNIX Way is about, so in this respect your approach is fine.

Name: Anonymous 2012-02-08 9:21

OK, I kind of did it.

Put this in your .bashrc:

export WELCOMEPWD=`mktemp -t`
export PS1='$(~/bin/welcome.sh)'$PS1


Put this in ~/bin/welcome.sh:

#!/bin/sh
if [ -n "$WELCOMEPWD" -a -f "$WELCOMEPWD" ] ; then
    if [ "$PWD" != "`cat "$WELCOMEPWD"`" ] ; then
        if [ "$PWD" = "$HOME" ]; then
            echo -e "Welcome home, oniichan!\n\n "
        fi
        echo "$PWD" > "$WELCOMEPWD"
    fi
fi


The problem is that fucking space, because otherwise fucking bash strips trailing newlines from anything that executes inside $() or ``, and this can't be disabled. So you have to put the beginning of your prompt in the script/command.

Name: Anonymous 2012-02-08 12:53

echo "fuck you japan faggot"

Don't change these.
Name: Email:
Entire Thread Thread List