Python Forum
writing all new scripts in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing all new scripts in Python
#2
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
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 code
Output:
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.
Reply


Messages In This Thread
writing all new scripts in Python - by Skaperen - Nov-09-2019, 03:46 AM
RE: writing all new scripts in Python - by Gribouillis - Nov-09-2019, 08:10 AM
RE: writing all new scripts in Python - by Larz60+ - Nov-09-2019, 11:16 AM
RE: writing all new scripts in Python - by Skaperen - Nov-10-2019, 01:33 AM
RE: writing all new scripts in Python - by Larz60+ - Nov-10-2019, 02:43 AM
RE: writing all new scripts in Python - by Skaperen - Nov-10-2019, 09:07 PM
RE: writing all new scripts in Python - by Skaperen - Nov-10-2019, 11:24 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020