再び探さなくても良いようにメモします。
`date '+%Y%m%d'`
紹介URL http://d.hatena.ne.jp/zariganitosh/20100921/get_file_name_ext_dir
fpath='/a/b/c.d.e'
fname_ext="${fpath##*/}" echo $fname_ext
c.d.e
fname="${fname_ext%.*}" echo $fname
c.d
fext="${fpath##*.}" echo $fext
e
fdir="${fpath%/*}" echo $fdir
/a/b
同じプロセス内で実行されるので、ディレクトリの移動をおこなうと関数の終了時にカレントディレクトリが変更される。 作業の環境変数自体をなんとかしたい場合にシェルスクリプトをつかうよりも有効なのだろうと思う。
なので、
という作りが便利なのだろう。
function myfunc() { }
関数の内部はインデントをつかうことで読みやすさが格段にアップする。
ファイルの検索結果に対していろいろ処理をしたい場合、ディレクトリ構造をそのまま保存しての処理がBSD版のcpコマンドでは難しかったりするが、下記のようにwhile read 変数名; do とすることで対処できる。
find . -type f ! -path "*a.txt" ! -path "*b.txt" ! -path "*c.txt" | while read f; do echo $f mkdir -p $mergedir/${f%/*} cp -a $f $mergedir/${f%/*} done echo "ok"
http://www.bulkitem.com/2011/09/blog-post.html
#!/bin/sh curl -O 'http://domain/1.jpg?1' & # 1M程度の画像 PID=$! curl -O 'http://domain/2.jpg?2' & # 1M程度の画像 wait $PID wait $! echo "END"
コマンドの履歴を重複を削って並べる
history | awk '{ $1=""; print $0}'|sort|uniq
find . -name "*.csv" | xargs grep -l test | ruby -i -ep 'gsub!(/test/,"tttt")'
http://d.hatena.ne.jp/Hoshi-KN/20120129/1327834914
#!/bin/sh
path_tool1=/opt/tool1 #PATHに上記のpath_tool1で指定したパスがあれば削除 if [ `echo $PATH | grep -e $path_tool1` ] ; then PATH=`echo $PATH | sed -e "s@$path_tool1:\{0,1\}@@"` export PATH fi unset path_tool1
path_tool2=/opt/tool2 #PATHに上記のpath_tool2で指定したパスがなければ追加 if [ ! `echo $PATH | grep -e $path_tool2` ] ; then PATH=$path_tool2:$PATH export PATH fi unset path_tool2
DATE=`date +'%Y%m%d-%H%M'`
function foo() { local arrayname=$1 eval ref=\"\${$arrayname[@]}\" local list=( ${ref} ) for i in "${list}"; do echo "${i}" done } hoge=( 'xxx' 'yyy' 'zzz' ) foo hoge
小粋空間
http://www.koikikukan.com/archives/2013/09/13-025555.php
#!/bin/sh TARGET=$1 if [ "$TARGET" == "" ]; then echo "パラメータを指定してください。"; elif [ -f $TARGET -o -d $TARGET ]; then TIMESTAMP=`ls -d -l --time-style='+%Y%m%d' $TARGET | awk '{print $6; }'`; TARGET_PATH=`dirname $TARGET` TARGET_BODY=`basename $TARGET` if [ "$TARGET_PATH" != "" ]; then TARGET=$TARGET_PATH/$TARGET_BODY else TARGET=$TARGET_BODY fi echo "mv $TARGET $TARGET.$TIMESTAMP" #mv $TARGET $TARGET.$TIMESTAMP else echo "指定されたファイルまたはディレクトリは存在しません。"; fi
http://typista.it-creates.com/dos/304.html
いままで、学んできたことを組み合わせて、シェルの関数を作りました。
function logmove(){ DATE=`date +'%Y%m%d'` \ls -1 *.log | while read TARGET;do FILEDATE=`ls -d -l --time-style='+%Y%m%d' $TARGET | awk '{print $6; }'`; FILEDAY=`ls -d -l --time-style='+%d' $TARGET | awk '{print $6; }'`; FILEMONTH=`ls -d -l --time-style='+%m' $TARGET | awk '{print $6; }'`; FILEYEAR=`ls -d -l --time-style='+%Y' $TARGET | awk '{print $6; }'`; if test $FILEDATE != $DATE ; then mkdir -p ./$FILEYEAR/$FILEMONTH/$FILEDAY echo mv ./$TARGET ./$FILEYEAR/$FILEMONTH/$FILEDAY/ fi #echo $FILEDATE done }
for X in `echo ${STOCKSHOP_AGENT} | tr -s ',' ' '`; do sh ${JENKINS_HOME}/JenkinsTools/sh/release_detail_data_reset.sh ${stockshop_flg} ${STOCKSHOP_SEASON} ${X} ${skill_flg} 0 0 ${upgrade_flg} 0 0 done
function grp(){ find . -type f | xargs grep --color -n $1 }
function git_init(){ git init git commit --allow-empty -m 'first commit' git checkout -b develop }
function mkb(){ echo '現在のブランチ': `git rev-parse --abbrev-ref @` if [ `git rev-parse --abbrev-ref @` = "develop" ]; then git pull git checkout -b $1 else echo "developブランチに切り替えてサイド実行してください" echo "git checkout develop" fi }
function mid(){ head -n $3 $1 | tail -n $(($3 - $2 + 1)) }
git config --global core.quotepath false
git symbolic-ref --short HEAD
git push --set-upstream origin `git symbolic-ref --short HEAD`
git config --global push.default current
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
git pull --rebase origin develop
Dockerで、複数サーバを立ち上げ、それぞれのサーバの出力を見るため、
複数ターミナルで立ち上げたい。
そんな時に使える
以下を ~/new.sh として保存
#!/bin/sh test $# -eq 0 && set -- : osascript - "$(pwd)" "$@" <<\EOF on run args set dir to quoted form of (first item of args) set cmd_strs to {} repeat with cmd_str in rest of args set end of cmd_strs to quoted form of cmd_str end set text item delimiters to " " set cmd to cmd_strs as Unicode text tell app "Terminal" to do script "cd " & dir & " && " & cmd end EOF
chmod +x ~/new.sh
#!/bin/sh cd ~/prj1 ~/new.sh rails s -p 3000 ~/new.sh rails s -p 3001
https://teratail.com/questions/12738
source コマンドを使えば可能
ちなみに、自分は、.bash_profileに
alias sb='soursce ~/.bash_profile'
エイリアスを入れている
https://qiita.com/rsooo/items/ef1d036bcc7282a66d7d
日本語を文字化けさせないため、一時ファイルを経由させる
#一時ファイルを作る tmpfile=$(mktemp) echo '{"id":5,"name":"りんご","price":1000,"imgPath":"peach.jpg"}' > $tmpfile #生成した一時ファイルを削除する function rm_tmpfile { [[ -f "$tmpfile" ]] && rm -f "$tmpfile" } # 正常終了したとき trap rm_tmpfile EXIT # 異常終了したとき trap 'trap - EXIT; rm_tmpfile; exit -1' INT PIPE TERM curl http://localhost:8080/api/items -i -XPOST -H 'Content-Type: application/json' --data-binary @"$tmpfile" rm_tmpfile "$tmpfile"