Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

cd from within sub #29

Open
cmalven opened this Issue · 6 comments

4 participants

Chris Malven Mislav Marohnić Guillaume jeffreyroberts
Chris Malven

This isn't so much an issue as a plea for assistance.

I'm working on a sub script that I can use for managing my various projects. One aspect of that involves navigating to the directory for a project.

I would use it like so:

cmal proj my-project

This command opens the project folder in Sublime Text, opens the dev url for the project in my browser, and ideally would cd into the directory for this project.

That last part is the tricky one, because the cd command is being run in a subshell, so once the cmal proj script finishes running I'm still in the same directory I started in.

Any tips on getting around this within the workings of sub?

In case it's not obvious, I'm a designer, not a command line guru. I know that normally I could get around this by using source my-script or . my-script but that's about all I know. Thanks.

Mislav Marohnić

If you want to change the state of you current shell, like set an environment variable or change directory, you can't call a command to do that (as you've already discovered). It has to be handled by a shell function or eval.

Fortunately, sub supports this via "sh-*" commands. Write a command as you normally would that starts with "sh-", and that command should output shell commands that will get evaled in your shell by sub.

Take a look at "cd" command in mislav/coral

Chris Malven

Fantastic! Thanks @mislav

Is there any way to prevent parts of my script from being evaled? For instance, my script drops into Ruby to do some side work and put things to the screen, and this seems to be screwing up my sh-proj command.

Alternatively, does sub let you call one of it's commands from within a sub command? For instance, could I call sh-cd from within proj?

Mislav Marohnić
Guillaume

Hi

Thanks for the explain but I'dont understand how to cd to a directory in a sub command

Edit now it can cd. But I've lost autocompletion.

Here what I've tried so far

My command is name tech-sh-open.

# Provide tech completions                                                                                                                                                                                                                                                  
if [ "$1" = "--complete" ]; then                                                                                                                                                                                                                                            
  find ~/dev/{plugins,projets,r-et-d} -mindepth 0 -maxdepth 1 ! -name '.*'  -exec basename {} \;                                                                                                            
fi   dir=$(find ~/dev -mindepth 1 -maxdepth 2 -name "$1" | head -n 1)

if [ -e "$dir" ]; then
  { [ -d "$dir/trunk" ] && app="$dir/trunk"; } || { [ -d "$dir/webapp" ] && app="$dir/webapp"; }
  echo cd ${app}"
else
  echo "Aucun projet '$1' trouvé..." >&2
  exit 1
fi

Any hints on the possible error ?

Guillaume

searching through github lead me to

cybera/sub@bd1c3eb which seem to allow autcompleting sh-* commands.
It work on my command

jeffreyroberts

I was able to do this by

cd $dir
bash

of course you have to type exit to end the execution of the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.