2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bash Script の自分的まとめ

Last updated at Posted at 2018-05-11

Summary

bash script の自分的まとめ
bash script むずかしい・・・

Environment

$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.13.4
BuildVersion:   17E202

01. Setting of Bash

Update bash

# 新しいbashをインストール
$ brew install bash

# インストールしたshellのパスを追加する
$ sudo vim /etc/shells
    /usr/local/bin/bash

# ターミナルのデフォルトのshellを設定する
$ chsh -s /usr/local/bin/bash

# 使われているshellの確認
$ echo $SHELL
/usr/local/bin/bash

$ echo $BASH_VERSION 
    4.4.19(1)-release

Config files

File Name Description
.bash_profile ログイン時に1回だけ実行される。
.bashrc シェルを起動するたびに実行される。localな感じ?
.inputrc ここはぜひとも変えておく!
.bash_profile
# いろいろ設定
# set path とか prompt とか completions とか
# たとえば

export EDITOR=vim

# ( language )
export LANG=en_us.UTF-8

# ( ls )
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

#-------------------------------------------------------------
#  Load .bashrc
#-------------------------------------------------------------

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
.bashrc
# いろいろ
# たとえば

export MONO_GAC_PREFIX=/usr/local
export WINEPREFIX=~/.wine
export XDG_CONFIG_HOME=~/.config
export FZF_DEFAULT_OPTS='--height 40% --reverse --border --ansi'
eval "$(hub alias -s)"
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
.inputrc
# see also: https://wiki.archlinux.jp/index.php/Readline

$include /etc/inputrc

set editing-mode vi

$if mode=vi

    set keymap vi-command
    # these are for vi-command mode
    "\C-p": history-search-backward
    "\C-n": history-search-forward
    j:history-search-forward
    k:history-search-backward

    set keymap vi-insert
    # these are for vi-insert mode
    "\C-p": history-search-backward
    "\C-n": history-search-forward
    "\C-h": backward-char
    "\C-l": forward-char

$endif

# requires >= Bash 4.3
set show-mode-in-prompt on
set show-all-if-ambiguous on
set completion-ignore-case on
# Color files by types
set colored-stats On
# Append char to indicate type
set visible-stats On
# Mark symlinked directories
set mark-symlinked-directories On
# Color the common prefix
set colored-completion-prefix On
# Color the common prefix in menu-complete
set menu-complete-display-prefix On

02. Reference of Bash

read! read! read!

$ man bash
$ info bash

03. Setting of Bash script

Run Bash script

$ vim foo.bash
    echo $BASH_VERSION

$ bash foo.bash
    4.4.19(1)-release

Run Bash script ( in Vim with vim-quickrun )

.vimrcは下記のようにする

.vimrc
" see also: :help ft-sh-syntax
autocmd FileType sh let g:is_bash = 1
autocmd FileType sh let g:sh_no_error = 1

let g:quickrun_config.sh = {
    \ 'command': 'bash'
\}

:QuickRun

$ vim foo.bash
    echo $BASH_VERSION

:QuickRun
    4.4.19(1)-release

shebangを書く時は下記のように書くのが無難

$ vim foo.bash
    #!/usr/bin/env bash
    echo $BASH_VERSION

:QuickRun
    4.4.19(1)-release

shebangで実行shellを指定できる

$ vim foo.bash
    #!/bin/bash
    echo $BASH_VERSION

:QuickRun
    3.2.57(1)-release

Auto-completet

terminalでのauto-complete

事前に $ brew install git をしておく

.bash_profile
# Bash's auto-complete is already implimented. ( requires > 4.x )
function bashComplete () {

    # Load extra complete config files
    local dir=$(brew --prefix)/etc/bash_completion.d
    for file in ${dir}/* ; do
        [ -r "$file" ] && source "$file"
    done
    unset file

}

bashComplete

vimでのauto-complete

" 調べ中〜 (^_^;;;

04. Bash script cheatsheet

Must see: Bash scripting cheatsheet

Basic

comment

# comment line

commnad

ls                  # ls is command (コマンドはstatus codeをかえす success 0, failed others )
ls >/dev/null 2>&1  # >/dev/null 2>&1 show gabage (表示を出力しない)
echo $?             # $? shows previous status code ( success 0, failed others )

variable, array and function

# variable
foo='
    foo
    bar
    baz
'

# array
bar=(
    foo
    bar
    baz
)

# function ( available local variables )
baz () {
    local foo=hello
    echo "${foo}"
}

# expansion $
echo "${foo}"
echo "${bar[@]}"

# run function
returnValue=$( baz )
echo ${returnValue} world

result


    foo
    bar
    baz

foo bar baz
hello world

condition if

see also : $ help test
ifstatus codeをみている

# lsコマンドが正常に終了したかどうか?
ls >/dev/null 2>&1
if [ "$?" = 0 ] ; then
    echo 'success!'
else
    echo 'failed!'
fi

# コマンドがあるかどうか?
if type foo >/dev/null 2>&1 ; then
    echo 'foo command exists!'
else
    echo 'foo command not exists!'
fi

# ファイルがあるかどうか?
if [ -e ./foo.txt ]; then
    echo "foo.txt found"
else
    echo "foo.txt NOT found."
fi

# フォルダがあるかどうか?
if [ -d ./foo/ ]; then
    echo "folder found"
else
    echo "folder NOT found."
fi

Loop ( while and for )

see also: $ help read

foo='
    foo     bar
    bar     bar
    baz     bar
'

echo "${foo}" |
    while read -r line
    do
        echo "${line}"
    done

for v in "${foo}"
do
    echo "${v}"
done

result


foo     bar
bar     bar
baz     bar


    foo     bar
    bar     bar
    baz     bar

Quoting and IFS

$ man bash
    /quoting

Quoting  is used to remove the special meaning of certain characters or
words to the shell.  Quoting can be used to disable  special  treatment
for special characters, to prevent reserved words from being recognized
as such, and to prevent parameter expansion.

$ man bash
    /ifs

The  Internal  Field  Separator  that is used for word splitting
after expansion and to split lines  into  words  with  the  read
builtin  command.   The  default  value  is  ``<space><tab><new-
line>''.
Name Example Exception
single quote 'foo' None
double quote "foo" $ \ `
back slash /$ None
foo='
    foo
    bar
    baz
'

# IFSがキャンセルされる
echo "${foo}"

#
#    foo
#    bar
#    baz
#

# IFSが効いている
echo ${foo}

#foo bar baz

Subshell

tmp$ cd foo/ ; ls
FSharp.Core.dll foo.exe         foo.exe.mdb     foo.fsx
foo$ pwd
/Users/callmekohei/tmp/foo

tmp$ ( cd foo/ ; ls )
FSharp.Core.dll foo.exe         foo.exe.mdb     foo.fsx
tmp$ pwd
/Users/callmekohei/tmp

Positional Parameters

position meaning
$0 file name
$@ all parameters
"$@" all parameters( turn off IFS )
$1 first parameter
$2 second parameter
for v in $@
do
    echo "${v}"
done

echo '-----'

for v in "$@"
do
    echo "${v}"
done

result

$ bash foo.bash 'hello world' 'foo bar baz'

hello
world
foo
bar
baz
-----
hello world
foo bar baz

Command substitution

$ man bash
    /command substitution

Command substitution allows the output of a command to replace the com-
mand name.  There are two forms:

          $(command)
    or
          `command`
# ただのlsという文字列
foo=ls
echo "${foo}"

# ls

# ls コマンド
bar=$(ls)
echo "${bar}"

# Desktop
# Documents
# Downloads
# Library
# Movies
# Music
# Pictures
# Public
# mono64
# tmp
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?