How to write a beautifully elegant linux command in a bash shell

I am running several web applications in my virtual machine, which is located on Ubuntu 12.04.

I find that I type the following very often when I enter my virtual machine.

  • cd /var/virtual/app1.com/
  • cd /var/virtual/app2.com/
  • cd /var/virtual/app3.com/

Each line represents a separate webapp root directory for specific instructions.

This is a lazy programmer asking if there is a way to type input.

  • go_app1
  • go_app2
  • go_app3

I know how to do this using a bash script.

but in the end I will type

./GoApp.sh -app app1

I want to make it more elegant just by typing go_app1.

Tip

EDIT:

I used the highest rating and I got this error message in Ubuntu.

https://gist.github.com/simkimsia/7336019

Where am I wrong?

EDIT2:

, , .bashrc.

+4
6

, , . , .

function go_app() { cd /var/virtual/app$1.com/; }

:

go_app 1

~/.bashrc.

+3

, .bash_profile.

alias go_app1='cd /var/virtual/app1.com/'

go_app1, .

+2

bash ? , Ctrl-R. , cd-ed app1.com, Ctrl-R, app1.com , app1.com, enter . "cd" , Ctrl-R , .

: http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/

, , , , , "cd" , app1.com "( ).

+2

$CDPATH ( bash) , (), :

 export CDPATH=.:/var/virtual:<other-dirs>

:

 cd app1.com

, , ( )

0

corkin, "" ( app1.com, app2.com,...), :

alias go_app='cd /var/virtual/; cd '

go_app app1.com

cd /var/virtual/app 1.com/. : cd_app. go_app ( cd ).

0

.bashrc:

_make_go_apps() {
    local r=/var/virtual
    local d qd
    while read d; do
        printf -v qd '%q' "$r/$d"
        eval "go_${d%.com}() { cd -- $qd; }"
    done < <( find "$r" -maxdepth 1 -regex ".*/app[0-9]+\.com" -type d -printf '%f\n')
}

go_appX X , /var/virtual/appX.com , cd .

( , ):

# mkdir -p /var/virtual/app{1..50}.com
# _make_go_apps
# go_app42
# pwd
/var/virtual/app42

!

, _make_go_apps.

OP , .

eval , ( , , , , , ).

, , : , , , ..

.

  • /var/virtual, r, , .
  • ( ).
  • , _clean_go_apps go_appX. ( .bashrc):

    clean_go_apps() {
        local r=/var/virtual
        local f
        while read _ _ f; do
            [[ $f =~ ^go_app[[:digit:]]+$ ]] || continue
            [[ -d $r/${f#go_}.com ]] && continue
            unset -f $f
        done < <(declare -F)
    }
    

    .


, /var/virtual , , .bashrc:

readonly _go_apps_basedir=/var/virtual
make_go_apps() {
    local d qd
    while IFS= read -d '' d; do
        printf -v qd '%q' "$_go_apps_basedir/$d"
        eval "go_${d%.com}() { cd -- $qd; }"
    done < <( find "$_go_apps_basedir" -maxdepth 1 -regex ".*/app[0-9]+\.com" -type d -printf '%f\0')
}

clean_go_apps() {
    local f
    while read _ _ f; do
        [[ $f =~ ^go_app[[:digit:]]+$ ]] || continue
        [[ -d "$_go_apps_basedir/${f#go_}.com" ]] && continue
        unset -f $f
    done < <(declare -F)
}

make_go_apps

_go_apps_basedir readonly, . IFS='' -d '' read -printf '%f\0' find, , find.


find _make_go_apps, 100% bash. 100% bash:

readonly _go_apps_basedir=/var/virtual
make_go_apps() {
    local f d qd saved_shopt
    saved_shopt=$(shopt -p)
    shopt -s extglob nullglob
    for d in "$_go_apps_basedir"/app+([[:digit:]]).com/; do
        f=${d#$_go_apps_basedir/}
        printf -v qd '%q' "$d"
        eval "go_${f%.com/}() { cd -- $qd; }"
    done
    eval "$saved_shopt"
}

clean_go_apps() {
    local f
    while read _ _ f; do
        [[ $f =~ ^go_app[[:digit:]]+$ ]] || continue
        [[ -d "$_go_apps_basedir/${f#go_}.com" ]] && continue
        unset -f $f
    done < <(declare -F)
}

make_go_apps

... ( !), , .


Edit Have . Now, to make this post even longer, here it is the same, but it will create a function go_Xfor each directory X.cominside /var/virtual, so that Xcontains only the characters: [0-9a-zA-Z\-\._]. Put this in your .bashrc:

readonly _go_apps_basedir=/var/virtual
make_go_functions() {
    local f d qd saved_shopt
    saved_shopt=$(shopt -p)
    shopt -s extglob nullglob
    for d in "$_go_apps_basedir"/+([[:alpha:][:digit:]\-\._]).com/; do
        f=${d#$_go_apps_basedir/}
        printf -v qd '%q' "$d"
        eval "go_${f%.com/}() { cd -- $qd; }"
    done
    eval "$saved_shopt"
}

clean_go_apps() {
    local f
    while read _ _ f; do
        [[ $f =~ ^go_.+$ ]] || continue
        [[ -d "$_go_apps_basedir/${f#go_}.com" ]] && continue
        unset -f $f
    done < <(declare -F)
}

make_go_functions
0
source

All Articles