Executing single function from Shell script in Windows GitBash closes shell unexpectedly - bash

I have recently installed Git For Windows version 2.19.1.windows.1 in my Windows 64Bit (both Windows 7 and 10, in two systems).
Now I have created below script to do some regular tasks easily w/o writing every instruction separately:
#!/bin/bash
## Contains functions and methods that can be executed inside Vagrant SSH Session
export SCRIPT_PATH="$(cd "$(dirname "$0")"; pwd -P)"
export PROJECT_ROOT_PATH="/var/www" PROJECT_ROOT_DIR="/var/www" ROOT_FOLDER="/var/www"
function fcc() {
echo "########## Frontend Cache clear begins #############"
[[ "$PWD" =~ "frontend/app" ]] && cd frontend/app
rm -rf webroot/cache_js && rm -rf webroot/cache_css
mkdir -m 777 webroot/cache_js && mkdir -m 777 webroot/cache_css
Console/cake AssetCompress.AssetCompress build -f
cat /dev/null > ~/.bash_history && history -wc && history -cw && exit
cd /var/www
echo "########## Frontend Cache clear ends #############"
keep_shell_open
}
function bocc() {
...
}
function bcc() {
...
}
function succ() {
...
}
function keep_shell_open() { exec $SHELL; };
Now when goto script's directory thro' Gitbash terminal & I register the script as below it unexpectedly opens the C:\Program Files\Git\usr\bin directory of GitBash:
. ./VagrantGuestScript.sh
And then when I execute the "fcc" function of this script, it only executes first 2 or 3 lines and then closes the terminal.
Can anyone explain why it opens Gitbash's bin path when trying to register the script & why it closes the terminal after executing only 2/3 lines of the function "fcc" ?

Related

How to run a function when the cd command is run

I am kind of new to Linux and am just learning to make the computer do the work that I want.
So my wish is that whenever I use the cd
command, I want it to change directory and then list all the files present in them. If there are no arguments passed, I want the pwd command to run.
This is what I have done so far.
function cd {
if [ $# -eq 0 ]
then
pwd
else
cd "$1"; ls -l
fi
}
When I run this, it works fine when there are no arguments passed and it runs the pwd command. However, when I pass an argument, it does not display anything and it closes the terminal, which is'nt what I want.
When I changed the function name to ca though, and ran ca, it worked as expected.
Why is this so? Are there a list of aliases I am not allowed to use? How can I make it work?
If it exists, chpwd_functions is an array of function names, each of which will be called, in order, whenever the working directory changes. In your case, it could be used as follows:
foo () {
if [[ $PWD == $HOME ]]; then
pwd
else
ls -l
fi
}
chpwd_functions+=(foo)
You recursively call your function instead of calling the cd builtin.
In ZSH the builtin command can be used to execute a builtin explicitly suppressing shell function lookup. This is exactly what you need to implement a function that has the same name as a shell builtin
function cd {
if [ $# -eq 0 ]
then
pwd
else
builtin cd "$1" ; ls -l
fi
}
This applies to BASH as well.
Regarding the command builtin
In BASH you could use the command builtin to execute an external command or a builtin. That is where BASH is different from ZSH as in ZSH command executes external commands only.
Only in BASH
command cd works the same as builtin cd (assuming /bin/cd does not exist)
In ZSH
command cd would probably fail with cd: command not found unless /bin/cd exists
The "cd" command in Bash can take options, so using just $1 will drop the directory name. This passes everything:
function cd {
if [ $# -eq 0 ]
then
pwd
else
cd "$#"; ls -l
fi
}
I was having a similar issue having switched from bash to zsh. Example function...
go() {
if [[ $# == "code" ]]; then command cd "$CODE";
}
The solution for me was to 1. prefix my function with function and 2. replace command with builtin
function go() {
if [[ $# == "code" ]]; then builtin cd "$CODE";
}

unset a function for one line in a bash script

In a script, I've defined: rm() { echo "rm $#" }
Normally rm would run echo rm, but I want to change it for a single line to run the actual rm command. How can I do this?
I've tried:
rm= rm file but this still runs echo rm file
I don't want to unset rm because I still want to be able to run rm file and expect it to echo.
Note: rm is just an example command, not the command I'm actually using.
You can use command to run a command instead of a function by the same name:
#!/bin/bash
hostname() {
echo "You are in a maze of twisty little passages, all alike. "
}
hostname # runs the function
command hostname # runs the external command

Activating virtualenv in Bash script not working

Writing a script to automate my flask environment setup.
if [[ -z $1 ]];
then
echo "usage: flaskup <dirname> <template dir>";
exit
else
virtualenv $1 &&
cd ./$1 &&
source bin/activate &&
bin/pip install flask &&
mkdir ./app &&
mkdir ./app/static &&
mkdir ./app/templates &&
exit;
fi
I'm expecting this to leave me in the directory it created, with the virtual environment activated, however it leaves me in the same directory I ran the script from. What can I do to make the script exit with the shell in the activated virtual environment?
If you run the script in its own shell (run it as /path/to/script or script if it lives in your $PATH) then you can't get what you want. The shell that runs the script is a different shell then the one you ran it from and it cannot change the status of the parent shell. The closest you could do would be to have the script echo the path as output and run it as cd "$(/path/to/script)" or similar.
Alternatively, if you run the script as . /path/to/script (or similar) then you are running it with your current shell and any directory changes it makes will be happening in your current shell and not a sub-shell.

Bash script to change parent shell directory [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 7 years ago.
What I'm trying to do
I've created a shell script that I've added to my $PATH that will download and get everything setup for a new Laravel project. I would like the script to end by changing my terminal directory into the new project folder.
From what I understand right now currently it's only changing the directory of the sub shell where the script is actually running. I can't seem to figure out how to do this. Any help is appreciated. Thank you!
#! /usr/bin/env bash
echo -e '\033[1;30m=========================================='
## check for a directory
if test -z "$1"; then
echo -e ' \033[0;31m✖ Please provide a directory name'
exit
fi
## check if directory already exist
if [ ! -d $1 ]; then
mkdir $1
else
echo -e ' \033[0;31m✖ The '"$1"' directory already exists'
exit
fi
# move to directory
cd $1
## Download Laravel
echo -e ' \033[0;32m+ \033[0mDownloading Laravel...'
curl -s -L https://github.com/laravel/laravel/zipball/master > laravel.zip
## Unzip, move, and clean up Laravel
echo -e ' \033[0;32m+ \033[0mUnzipping and cleaning up files...'
unzip -q laravel.zip
rm laravel.zip
cd *-laravel-*
mv * ..
cd ..
rm -R *-laravel-*
## Make the /storage directory writable
echo -e ' \033[0;32m+ \033[0mMaking /storage directory writable...'
chmod -R o+w storage
## Download and install the Generators
echo -e ' \033[0;32m+ \033[0mInstalling Generators...'
curl -s -L https://raw.github.com/JeffreyWay/Laravel-Generator/master/generate.php > application/tasks/generate.php
## Update the application key
echo -e ' \033[0;32m+ \033[0mUpdating Application Key...'
MD5=`date +”%N” | md5`
sed -ie 's/YourSecretKeyGoesHere!/'"$MD5"'/' application/config/application.php
rm application/config/application.phpe
## Create .gitignore and initial git if -git is passed
if [ "$2" == "-git" ]; then
echo -e ' \033[0;32m+ \033[0mInitiating git...'
touch .gitignore
curl -s -L https://raw.github.com/gist/4223565/be9f8e85f74a92c95e615ad1649c8d773e908036/.gitignore > .gitignore
# Create a local git repo
git init --quiet
git add * .gitignore
git commit -m 'Initial commit.' --quiet
fi
echo -e '\033[1;30m=========================================='
echo -e ' \033[0;32m✔ Laravel Setup Complete\033[0m'
## Change parent shell directory to new directory
## Currently it's only changing in the sub shell
filepath=`pwd`
cd "$filepath"
You can technically source your script to run it in your parent shell instead of spawning a subshell to run it. This way whatever changes you make to your current shell (including changing directories) persist.
source /path/to/my/script/script
or
. /path/to/my/script/script
But sourcing has its own dangers, use carefully.
(Peripherally related: how to use scripts to change directories)
Use a shell function to front-end your script
setup () {
# first, call your big script.
# (It could be open-coded here but that might be a bit ugly.)
# then finally...
cd someplace
}
Put the shell function in a shell startup file.
Child processes (including shells) cannot change current directory of parent process. Typical solution is using eval in the parent shell. In shell script echo commands you want to run by parent shell:
echo "cd $filepath"
In parent shell, you can kick the shell script with eval:
eval `sh foo.sh`
Note that all standard output will be executed as shell commands. Messages should output to standard error:
echo "Some messages" >&2
command ... >&2
This can't be done. Use exec to open a new shell in the appropriate directory, replacing the script interpreter.
exec bash
I suppose one possibility would be to make sure that the only output of your script is the path name you want to end up in, and then do:
cd `/path/to/my/script`
There's no way your script can directly affect the environment (including it's current directory) of its parent shell, but this would request that the parent shell itself change directories based on the output of the script...

How to change current working directory inside command_not_found_handle

I'm trying to write a not found handle in Bash that does the following:
If $1 exists and it's a directory, cd into it.
If $1 exists inside a user defined directory $DEV_DIR, `cd into it.
If the previous conditions don't apply, fail.
Right now I have something like this:
export DEV_DIR=/Users/federico/programacion/
function command_not_found_handle () {
if [ -d $1 ]; then # the dir exists in '.'
cd $1
else
to=$DEV_DIR$1
if [ -d $to ]; then
cd $to
echo `pwd`
else
echo "${1}: command not found"
fi
fi
}
And although it seems to be working (the echo pwd command prints the expected dir), the directory in the actual shell does not change.
I was under the impression that since this is a function inside my .bashrc the shell wouldn't fork and I could do the cd but apparently that's not working. Any tips on how to solve this would be appreciated.
I think what's going on is that the shell fork()s after setting up any redirections but before looking for commands, so command_not_found_handle can't affect the interactive shell process.
What you seem to want to do may partly possible using the autocd feature:
shopt -s autocd
From man bash:
autocd - If set, a command name that is the name of a directory
is executed as if it were the argument to the cd com‐
mand. This option is only used by interactive shells.
Otherwise, just create a function that you invoke by name that performs the actions you are trying to use command_not_found_handle for.
It won't change directies if you run this program as a script in your main shell because it creates a sub-shell when it executes. If you source the script in your current shell then it will have the desired effect.
~/wbailey> source command_not_found.sh
That said, I think the following would achieve the same result:
wesbailey#feynman:~/code_katas> cd xxx 2> /dev/null || cd ..; pwd
/Users/wesbailey
just replace the ".." with your env var defined directory and create an alias in your .bashrc file.
I've had the very same wish and the solution that I've been using for a while was opening a new tab in gnome terminal by issuing the command gnome-terminal --tab --working-directory="$FOLDER" from inside the command_not_found handle.
But today I've come up with a solution which is not tied to a specific terminal application, but has exactly the intended behaviour.
The solution uses the PROMPT_COMMAND, which is run before each prompt. The PROMPT_COMMAND is bound to a function responsible for checking for a file related to current shell, and cd'ing into the directory specified in that file.
Then, the command_not_found_handle fills in the file when a change in directory is desired. My original command_not_found_handle also checkout a git branch if the current directory is a git repository and the name matches an existing branch. But to keep focus on answering the current question, I've stripped that part of code.
The command_not_found_handle uses find for searching for the directory matching the given name and goes only 2 levels deep in the directory tree, starting from a configured list.
The code to be added to bash_rc follows:
PROMPT_COMMAND=current_shell_cd
CD_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/bash-cd/$$.cd"
current_shell_cd() {
if [ -r "$CD_FILE" ]; then
local CD_TARGET="$( cat "$CD_FILE" )"
[ ! -z "$CD_TARGET" ] && cd "$CD_TARGET" 2>/dev/null
rm "$CD_FILE"
fi
}
command_not_found_handle () {
local COMMAND="$1";
# List folders which are going to be checked
local BASE_FOLDER_LIST=(
"$HOME/Desenvolvimento"
"/var/www/html"
"$HOME/.local/opt/"
)
local FOLDER=$(
find "${BASE_FOLDER_LIST[#]}" \
-maxdepth 2 -type d \
-iname "$COMMAND" -print -quit )
if [ ! -z "$FOLDER" -a -d "$FOLDER" ]
then
mkdir -p "$( dirname "$CD_FILE" )"
echo "$FOLDER" > "$CD_FILE"
else
printf "%s: command not found\n" "$1" 1>&2
return 127
fi
}

Resources