スキップしてメイン コンテンツに移動

投稿

ラベル(bash)が付いた投稿を表示しています

GitのCheckoutとCommitをするBashスクリプト

テキスト系のファイルをバックアップするために、Gitレポジトリを利用することがあるのですが、使いまわせるようにbashで簡単なスクリプトを書いてみました。 単純なバックアップ目的なので、branchは固定でmasterを指定しています。 #!/bin/bash checkout(){ local url=${1} local checkout_dir=${2} if [ ! -d $checkout_dir ] then git clone $url $checkout_dir else cd $checkout_dir git pull fi } commit(){ local commit_target_dir=${1} local message=${2} cd $commit_target_dir diff_count=$(echo `git status -s | wc -l`) if [ $diff_count -ne 0 ]; then git add -A git commit -m $message git push origin master fi } ### 使用例 ### USER=... PASSWORD=... # URL組み立て URL=https://$USER:$PASSWORD@githost/project checkout $URL "backup_directory" ### do something in backup_directory... commit "backup_directory" "Auto commit by bash" ちなみに筆者は、バックアップを下記の手順で取ってgitへ入れています。参考になれば、、、 上記のcheckout関数を用いて、gitレポジトリからプロジェクトをclone(既にclone済みの場合はpull) リモートホストからチェックアウトしたディレクトリへファイルをrsyncで同期 上記のcommit関数を用いて、gitレポジトリへpush

Linuxで特定のパターンにマッチするファイルの削除方法

Linuxで特定のパターンにマッチするファイルの削除方法について簡単にメモ 例1: /tmpディレクトリ配下の128bit hash(例:e111c876b32143abccd98bb56542bcc2)のパターンにマッチするファイルをすべて削除 regextypeを指定しないと我々が普段(?)よく知っているposixのregexが使えないことに注意! find /tmp -type f -regextype posix-egrep -regex '\./[0-9a-f]{32}' -delete 例2: /tmpディレクトリ配下の、AAAで始まるディレクトリで、更新日が5日以前のものを、ディレクトリの中身のファイルも含めてすべて削除 最後の「-exec {} +」は複数のファイルをまとめてコマンド実行するという意味 「-exec {} ;」の場合はファイル一個ずつの処理になるので、rmの場合は「-exec {} +」の方が効率が良い。 find /tmp -mindepth 1 -maxdepth -type d -mtime +5 -name ".AAA*" -exec rm -rf {} +

Bashでオプション引数をとる方法

下記の例で、変数A, B, Cにbashからオプション引数を割り当てる方法示します。 #!/bin/bash set -e set -o pipefail OPT=`getopt a:b:c: $*` set -- $OPT for i; do case $i in --) shift; break ;; -a) A=$2; shift; shift;; -b) B=$2; shift; shift;; -c) C=$2; shift; shift;; esac done # check if A, B and C are properly given from option. if [ -z "$A" ]; then echo "A is not specified"; exit; fi if [ -z "$B" ]; then echo "B is not specified"; exit; fi if [ -z "$C" ]; then echo "C is not specified"; exit; fi

Linuxで特定のディスクの使用率を調べるコマンド

Linuxで特定のディスクの使用率は下記のコマンドで調べられます。 df {対象のディレクトリパス} | awk '{print $5}' | sed -ne 2p | cut -d"%" -f1 応用で、Bashを使って下記の機能を実現させてみます。 対象ディレクトリを指定できるように 制限値より多い場合に実行失敗にする #!/bin/bash checkDiskUsage() { local $directory=$1 local $limit=$2 # 下記のコマンドの前にssh hostのようにすれば、リモートホストでも実行可能。 local used=`df $directory | awk '{print $5}' | sed -ne 2p | cut -d"%" -f1` echo "Disk usage of $directory: $used%" if [ $used -gt $3 ] then echo "Exceed limit of disk usage (greather than $limit%)" exit 1 fi } checkDiskUsage "/tmp" 80

Linuxでテキストファイルから重複行を取り除く方法

下記のコマンドで実現できます。 less {対象のテキストファイル} | sort -s -k 1 | uniq > result.txt コマンドの簡単な解説です。 lessコマンドで、対象のテキストファイルを読み込み sortコマンドで、1行目を指定してソート uniqコマンドで、重複を取り除く 結果をテキストに書き込み

Bash Script for Back Up Files to Subversion

Introduction There are 2 ways to control files on multiple machines in the software development world. Deploy or build file from version control system Backup files to version control system In my opinion, ideally, everything should be done by the way 1 because it ensures the files are same across the all machines. However in the real world, sometime it is hard to deploy all files from version control system because of the various reasons - such as linux/unix permission or organization structure or too difficult to automating deployment or etc. etc.... In that case, we should take the way 2. In this post, I will introduce simple bash script for back up files to Subversion. Of course you can use your favorite version control system such as Git instead of Subversion ;) If you are a software developer, you can easily refine my bash script and create git version quickly ;) Bash Script for Back Up Files to Subversion See below script! You just save the script and simply execut

Check Black List Words in Files by Bash Script

I wrote tiny bash script for checkin files contains black listed words or not. The aim of this script is to automate verifying php or javascript files does not have any debugging code, such as "var_dump","alert" etc. #!/bin/bash PHP_BLACK_LIST_WORDS=("var_dump(") JAVASCRIPT_BLACK_LIST_WORDS=("alert(") verify_words_are_not_contained(){ words="$1" len=${#words[@]} if [ $len -ge 0 ] then grep_words="" for word in "${words[@]}" do grep_words+="$word\|" done # remove last 2 characters grep_words=${grep_words%?} grep_words=${grep_words%?} grep_command="grep \"$grep_words\" $2 -nr --include=$3 > $OUTPUT_DIR/$4" echo "running $grep_command" eval $grep_command fi } setup_output_dir(){ target_dir_name=${1##*/} OUTPUT_DIR=$target_dir_name"_detected_black_list_words" echo "$OUTPUT_DIR" rm -r -f

Bash Script: Convert File Extension Lower Case

I wrote a small bash script for converting file extension to lower. #!/bin/bash PATH="/somwhere_in_the_world/" EXTS=("jpg" "gif" "png") for ((I=0; I<${#EXTS[@]}; ++I )) do FILES=`find $PATH ! -type f -iname "*."${EXTS[$I]}`; for FILE in $FILES; do FNAME=`expr $FILE : '\(.*\)\.\w\+'` LOWER=$FNAME"."${EXTS[$I]} if [ $FILE != $LOWER ]; then mv $FILE $LOWER echo "$FILE converted to lower case."; fi; done; done;

Creating SVN Tag in Bash

In this post, I will show you an example bash script for creating svn tag. First, if you would like to create tag on svn, you simply execute the following command. svn copy 'source_url' 'tag_url' Okay then, we simply call the above command in bash script. I use revision number and time stamp as tag name. Instead of using revision number, maybe you can prepare version number file and count up it when creating tag. #!/bin/bash SVN_USERNAME=dukesoftware SVN_PASSWORD=some_password SVN_INFO_COMMAND="svn info --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive" SVN_COPY_COMMAND="svn copy --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive" svn_project_trunk="http://somewhere.svn/project/trunk" svn_project_base=${svn_project_trunk/trunk/} # Getting revision from svn info command # Need this line for displaying message of svn info command in English forcibly. export LC_MESSAGES=C revision=`$SVN_INFO_CO

Taking Diff bewteen Two Repositories

I wrote a bash script for taking diffs between two repositories. #!/bin/bash SVN_USERNAME=dukesoftware SVN_PASSWORD=some_password SVN_COMMON_COMMAND="svn --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive --ignore-externals --quiet" SVN_COMMON_UPDATE_COMMAND="$SVN_COMMON_COMMAND update --set-depth infinity" SVN_COMMON_CHECKOUT_COMMAND="$SVN_COMMON_COMMAND co --depth immediates" SVN_BASE_URL=http://svn.repository.dummy.com/projects/ # comparison svn urls SVN_URL1=$SVN_BASE_URL/url1 SVN_URL2=$SVN_BASE_URL/url2 DIR1=target1 DIR2=target2 OUTPUT_DIR=diff_out EXCLUDE_FILES=(".svn" "jquery-*.min.js") # To minimize number of checkout files, set below constants CHECKOUT_DIRS=("dir1" "dir2" "lib") DIFF_TARGETS=("dir1/sub1" "dir1/sub2" "dir2" "lib") # function definitions build_base_diff_command(){ DIFF_BASE_COMMAND="diff -E -B -b -u -r" fo