Nov-09-2019, 08:10 AM
(This post was last modified: Nov-09-2019, 08:11 AM by Gribouillis.)
I tend to write as many scripts as I can in python. However, it is not always possible. For example by calling a python script, one cannot change the current directory in the calling shell. So one day I wanted to have a python script to define a extended version of the 'cd' command. The command I wrote is called 'va' which is the french imperative mood for 'go'. So if I type in a shell
It took me some research on the web to find out how to do this because I'm not very good at shell programming but I've been using this 'va' command for a few years now and it is very convenient.
Output:va spam
then the shell changes current directory to whatever directory I associate with the word 'spam'. One cannot do this with a python script, so I had to define 'va()' as a bash function defined in the file '~/.bashrc'. Here is its codeOutput:function va()
{
va_retour=$(python ~/bin/va.py $*)
echo $va_retour
if [[ ! -z "${va_retour// }" ]]
then
cd "$va_retour"
fi
}
When invoked, the function calls a python script with the command for example python ~/bin/va.py spam
. This python script prints the name of the target directory on its stdout, then bash does the 'cd'.It took me some research on the web to find out how to do this because I'm not very good at shell programming but I've been using this 'va' command for a few years now and it is very convenient.