Quantcast
Channel: Vimタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 5608

Bash Script の自分的まとめ

$
0
0

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 NameDescription
.bash_profileログイン時に1回だけ実行される。環境変数を書くものとして扱うのが良い
.bashrcシェルを起動するたびに実行される。callmekohei的には使ってない
.inputrcここはぜひとも変えておく!
.bash_profile
if[-f ~/.bashrc ];then. ~/.bashrc
fi# 言語の設定export LANG=en

# パスを通す# MacOSXは何もしなくてもパスが自動設定されている# see also: http://takuya-1st.hatenablog.jp/entry/2013/12/14/040814# $ man -k path_helper# $ man path_helper 8function setExtraPath (){# Homebrewのappにパスを通すlocal cellar=/usr/local/bin
    export PATH="${cellar}":"${PATH}"# opensslにパスを通すlocal openssl=/usr/local/opt/openssl/bin
    export PATH="${openssl}":"${PATH}"}

setExtraPath
.bashrc
# appから自動的に記述されるコマンドとか[-f ~/.fzf.bash ]&&source ~/.fzf.bash
.inputrc
set editing-mode vi

# requires >= Bash 4.3set show-mode-in-prompt on

set show-all-if-ambiguous on
set completion-ignore-case on

set keymap vi-command
    "\C-p": history-search-backward
    "\C-n": history-search-forward

set keymap vi-insert
    "\C-p": history-search-backward
    "\C-n": history-search-forward
    "\C-h": backward-char
    "\C-l": forward-char

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 FileTypeshletg:is_bash=1
autocmd FileTypeshletg:sh_no_error=1letg: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 bashecho$BASH_VERSION

:QuickRun
    4.4.19(1)-release

shebangで実行shellを指定できる

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

:QuickRun
    3.2.57(1)-release

Auto-completet

terminalでのauto-complete

$ brew install bash-completion@2
.bash_profile
# Add the following line to your ~/.bash_profile:if[-f /usr/local/share/bash-completion/bash_completion ];then. /usr/local/share/bash-completion/bash_completion
fi

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

# variablefoo='
    foo
    bar
    baz
'# arraybar=(
    foo
    bar
    baz
)# function ( available local variables )
baz (){local foo=hello
    echo"${foo}"}# expansion $echo"${foo}"echo"${bar[@]}"# run functionreturnValue=$( 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>''.
NameExampleException
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

positionmeaning
$0file name
$@all parameters
"$@"all parameters( turn off IFS )
$1first parameter
$2second 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

Viewing all articles
Browse latest Browse all 5608

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>