Bash脚本更改父Shell目录 [英] Bash script to change parent shell directory

查看:224
本文介绍了Bash脚本更改父Shell目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么

我已经创建了一个外壳脚本,并将其添加到$ PATH中,该脚本将下载并为新的 Laravel 项目进行所有设置. .我希望通过将终端目录更改为新的项目文件夹来结束脚本.

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.

据我目前的了解,它只是更改脚本实际运行所在的子shell的目录.我似乎无法弄清楚该如何做.任何帮助表示赞赏.谢谢!

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"

推荐答案

使用shell 函数来前端脚本

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
}

将shell函数放在shell启动文件中.

Put the shell function in a shell startup file.

这篇关于Bash脚本更改父Shell目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆