タイトルの通り、UNIXの「uniq」コマンドを使って、2つのファイルから 論理和(OR) 論理積(AND) 排他的論理和(XOR) 差集合(A-B) 差集合(B-A) を取得する方法を示します。 方法 前提:入力ファイル 確認のため、以下のような、ファイルa・bを使用します。
タイトルの通り、UNIXの「uniq」コマンドを使って、2つのファイルから 論理和(OR) 論理積(AND) 排他的論理和(XOR) 差集合(A-B) 差集合(B-A) を取得する方法を示します。 方法 前提:入力ファイル 確認のため、以下のような、ファイルa・bを使用します。
はい、おつカレー様です。 bashを使ったシェルスクリプトでパイプを使っても戻り値をちゃんと取りたいときの話です。 シェルスクリプトの戻り値 シェルスクリプトでの戻り値取るときに $? を使うことがあると思います。 $ ls / # こっちは正常 $ echo $? 0 $ ls /notfounddir # こっちはエラー $ echo $? 1コマンドのログを取りたいなと思ってteeとかつなげちゃうとteeの戻り値になっちゃって取れないですよね。 $ ls /notfounddir | tee test ls: /notfounddir: No such file or directory $ echo $? 0 # <-アレ? ${PIPESTATUS[@]} こういう場合は、${PIPESTATUS[@]}を使うことで取ることができます。 $ ls / | tee test.log
12.1. シェルスクリプトの中で対話的に処理をしたい¶ シェルスクリプトを書いている時、実行している人に複数ある処理のうちの1つを選んでもらいたい、なんて事がよくありませんか? 例えば、
シェルスクリプトは環境依存が激しいから…… などとよく言われ、敬遠される。それなら共通しているものだけ使えばいいのだが、それについてまとめているところがなかなかないので作ってみることにした。 「どの環境でも使える=POSIXで定義されている」と定義 「どの環境でも使える」とは、なかなか定義が難しい。あまりこだわりすぎると「古いものも含め、既存のUNIX全てで使えるものでなければダメ」ということになってしまう。しかし、私個人としては 今も現役(=メンテナンスされている)のUNIX系OSで使いまわせること にこだわりたい。 とはいっても全てのOSやディストリビューションについて調べられるわけではないので、この記事では基本的に最新のPOSIXで定義されていることをもって、どの環境でも使えると判断するようにした。(飽くまで「基本的に」ということで) 従って、互換性確保のため、シェルの中で使ってよい
乗算の演算子は「*」ですが、UNIXでは「*」はワイルドカードという特別な意味がありますので、エスケープ文字「\」を使用して「\*」と書きます。また、演算子の前後のスペースは省略できませんので注意してください。以下は四則演算のサンプルスクリプトです。 expr_test.sh #!/bin/sh a=`expr 5 + 3` b=`expr 5 - 3` ab=`expr $a + $b` c=`expr 5 \* 3` d=`expr 5 / 3` e=`expr 5 % 3` echo "a=$a" echo "b=$b" echo "ab=$ab" echo "c=$c" echo "d=$d" echo "e=$e"
変数 シェル変数 変数は文字列や数値など、さまざまなデータを記憶しておくものです。変数名に使える文字は、英数字または「 _ 」(アンダーバー)で、1文字目に数字は使えません。変数に値を代入するには「変数名=値」とします。「=」の前後にはスペースやタブを入れることはできません。変数を参照する場合は、変数名の前に「$」を付けます。下記の例を参考に解説します。 test.sh #!/bin/sh #-----変数に値を代入します----- PARA1="string" PARA2=string PARA3=3 PARA4=`date '+%Y-%m-%d'` #-----変数を「echo」で表示します----- echo "PARA1 = ${PARA1}" echo "PARA2 = ${PARA2}" echo "PARA3 = ${PARA3}" echo "PARA
こんにちは。 マネーフォワードでアグリゲーション開発を担当しています中川です。 今回のブログは、私が bash スクリプトを書く際に心がけている事のおさらいをします。 知ってて当たり前のことかも知れませんが、意外と理解されていないアレです。 では、私が bash スクリプトを書く際によく使う記述を一つずつ紹介します。 2種類の shebang シェルスクリプトの一行目に必ず記述する #! で始まる行を shebang と言います。 bash スクリプトの shebang は、bash を絶対パスで指定する方法と、env を使って指定する方法の二種類あります。 bash を絶対パスを指定する方法 #!/bin/bash env を使ってを指定する方法 #!/usr/bin/env bash 前者は /bin/bash が使われます。 (/bin/bash が存在しなければスクリプトの起動時に
業務で楽するためのUNIXテクニック集 ファイル・ディレクトリ名の一括置換/一括作成 この記事は2008年~2009年頃に翔泳社で連載していた「業務で楽するためのUNIXテクニック集」の原本です。 はじめに この連載では、UNIX系OSをコマンドライン上から効率よく利用するために知っておくべきテクニックや、便利な小技を紹介していきます。 今回は、第0回、第1回で紹介したfind/xargsコマンド、シェルの変数展開、制御構造を利用した「ファイル・ディレクトリ名の一括置換」に関するテクニックを紹介します。 また、「ファイルやディレクトリの一括作成方法」「シンボリックリンクの作成方法」に関するテクニックも紹介します。「ファイル名の置換」テクニックを検証する際の、コマンド確認環境構築にご利用ください。 関連記事 シェル制御構造と正規表現の基礎 grepを使った便利なコマンド一覧 find/xar
私はシェルスクリプトの大ファンで、他人のスクリプトから面白い方法を学ぶのが大好きだ。最近、SSHサーバの2要素認証を簡単にするためのauthy-sshスクリプトに出会った。このスクリプト群を見まわしていて、みんなと共有したいたくさんのクールなことを見つけた。 出力に色付けする 出力文字列を、成功した時は緑に、失敗した時は赤に、警告は黄色に色づけしたいと思うことはたくさんあるだろう。 NORMAL=$(tput sgr0) GREEN=$(tput setaf 2; tput bold) YELLOW=$(tput setaf 3) RED=$(tput setaf 1) function red() { echo -e "$RED$*$NORMAL" } function green() { echo -e "$GREEN$*$NORMAL" } function yellow() { e
シェルスクリプト Tips コーディング・スタイル ここに書かれている内容は、あくまで筆者の好みでありほぼ完全に主観ではあるが、経験的に行き着いたスタイルでもあるので推奨します。 スクリプトのヘッダを作成する 最近はあまり使用されることもないのかもしれないが @(#) の記述と、スクリプトの使用方法、および概要をファイルの前方にコメントとして記述しておくようにする。 ※ @(#) は what コマンドで参照する情報を記述するための記号です。詳細は「whatコマンドについて」を参照。 #!/bin/bash # # @(#) hoge.sh ver.1.0.0 2008.04.24 # # Usage: # hoge.sh param1 param2 # param1 - パラメータ1です. # param2 - パラメータ2です. # # Description: # hoge.shスク
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く