Bash Shell Scripting: what simple logic I am missing

This may be too general a question, like ... I'm at a dead end trying to move directories from a shell script. I am not a non-nix user, but I am more comfortable using the command line for most tasks. I would call a script that can move 'me' to a directory instead of a script process that would look like the following:

prompt:> goto lit 

where goto is an alias -> goto='./goscript'
and
goscript has some simple code, for example:

 cd /path to work dirs/lit/user dir 

(provided that each user has a directory inside / on)

I myself circumvented this problem by setting my personal alias to go to the correct directory, run the script, and then return to the original directory. This question was asked to me by an employee who uses a similar method, but wanted the process to become more general, so we do not need to create every single alias that we need. I thought this would be an easy problem to solve, but I'm at a standstill, because in reality I don't really have much experience with the shell ....

+6
linux bash shell pid
source share
9 answers

Even better than using an alias described by others, check the CDPATH variable! This is basically equivalent to the PATH functionality, but applies to the cd command.

For example, if I define my CDPATH as $CDPATH:${HOME}/subdir , and ~/subdir contains another directory, subsubdir , then I can simply do:

 cd subsubdir 

from any directory and move the path as expected.

Here are some more features:

http://www.caliban.org/bash/#bashtips

To set the CDPATH variable, add a line to your .bashrc , for example

 export CDPATH=$CDPATH:${HOME}/subdir 
+9
source share

You can create a function called goto (or something else) and make sure that it is defined in your .bashrc (or you can "fix" it from the current shell):

 function goto { # the "$USER" part will expand to the current username # the "$1" will expand to the first argument to the function ("goto xyz" => $1 is "xyz") cd /some-path/lit/$USER/$1 } 

Put this in ~ / .bashrc or in a separate file and call "source the-file" from your prompt, then you can call the function just like any other program:

 prompt> goto folder cd /some-path/lit/your-user/folder 
+5
source share

To execute the script in the same environment of your invitation, you must invoke it with.

Let's say you have a script named:

 cd .. 

if you call it with. You get:

 $> pwd $> /home/users/rd/proj $> . up $> pwd $> /home/users/rd 
+4
source share

You can not. The script has its own copy of the environment and therefore cannot change the environment of the login shell.

+2
source share

I was not a member when I asked about it (ironically, my anonymous login question has a higher talent than my official logical questions), so I canโ€™t close it now - But- The Remo D. et al. the answer would be what led to the working solution we needed. If someone with authority can close this and choose what is the accepted answer, I would deceive him.

+1
source share

You cannot use cd in a bash script. You could use the alias cd and path.

 alias goto='cd /path_to_work/usr/dir' 

UPDATE: you put this line in your .bashrc file and then do

 source ~/.bashrc 

to create an alias.

0
source share

You can do some simple things like this using an alias. For example, I have some aliases that put me in a workspace environment and set up variables for our Makefile system. Here is the real alias that I use as an example. "& &" will continue to execute commands until one of them works - in simple scripts like this, cleaning is not required because the failure is small.

 alias main1='cd ~/code/main1 && export TOP=`pwd` && export DEBUG=1' 

If you want to create a common library of aliases among your employees, you can share them through NFS and specify the file to be included.

0
source share

Each script has its own idea of โ€‹โ€‹the current working directory. "cd" is not a process, but an embedded shell.

The way to do what you want is to create an alias. In csh, it will be something like:

 alias goto 'cd /path_to_work/\!*/user_dir' 

sh / bash have similar alias facilities

0
source share

I tend to have dozens of aliases in my .rc that look like this:

 alias work='cd /home/foo/work' alias rails='cd /home/foo/rails' alias assets='cd /home/foo/rails/assets' 

and etc.

If this does not work for you, you can use the debug hooks in bash to replace the command line in place before executing it - if the keyword "goto" is the first thing on the line. This will include a google search for a "trap" in bash, but to be warned, such spells do not behave the same on all systems. I wrote one such experience a few months ago, and you can get the results .

0
source share

All Articles