2011年2月1日火曜日

シェルスクリプトサンプル:一括文字コード変換


#!/bin/sh

# 作業ディレクトリ(現在地)
BASE=`pwd`

# 変換対象元ディレクトリ
DIR=/home/efiles3/public/iw/072_STG-Web

# 変換対象ファイルの拡張子を指定する。
FILE_TYPE=".txt"

# 変換先文字コードオプション(UTF-8:-w SJIS:-s EUC:-e)
CHARA_CODE="-s"


# 出力先ディレクトリ作成
if [ ${CHARA_CODE} = "-w" ];then
OUTPUT_DIR="output_utf-8"
elif [ ${CHARA_CODE} = "-s" ];then
OUTPUT_DIR="output_sjis"
elif [ ${CHARA_CODE} = "-e" ];then
OUTPUT_DIR="output_euc"
else
echo "Character code is not existing error !!"
exit 0
fi
mkdir ./$OUTPUT_DIR

# 文字コード変換
cd $DIR
for i in `ls -1` ; do
/usr/bin/nkf $CHARA_CODE $i > $BASE/$OUTPUT_DIR/$i
done
cd $BASE


exit 0

2011年1月7日金曜日

シェルスクリプトサンプル:diffの差分比較結果をファイル出力

diffコマンドを利用して、2つのファイルの差分比較を行い、その結果を
ファイルに出力するシェルスクリプトのサンプルです。
Webコンテンツの移行作業において、移行前と移行後のファイルの差分が
発生していないか検証する際に便利です。


■サンプルスクリプト(sabun.sh)

[root@tanyao ~]# cat sabun.sh
#!/bin/sh

# 必ず比較対象ファイルと同階層にこのシェルを配置して実行してください!!

# 比較対象(パス指定禁止)
FILE1=file1.txt
FILE2=file2.txt

# ファイル名にスペースを含むファイルはNG扱いとする
if [ `grep " " ${FILE1} | wc -l` != 0 ] ; then
    grep " " ${FILE1} > diff_NG_${FILE1}
fi
if [ `grep " " ${FILE2} | wc -l` != 0 ] ; then
    grep " " ${FILE2} > diff_NG_${FILE2}
fi

# 並び替え
grep -v " " ${FILE1} | sort > sort_${FILE1}
grep -v " " ${FILE2} | sort > sort_${FILE2}

# 比較
diff -y -t -d sort_${FILE1} sort_${FILE2} > diff.txt

# 同一レコードの抽出
grep -v "<" diff.txt | grep -v ">" | grep -v "|" > diff_same.txt

# 第1ファイルにのみ存在
grep -e "<" -e "|" diff.txt | cut -d " " -f 1 > diff_only_${FILE1}

# 第2ファイルにのみ存在
grep ">" diff.txt | cut -d ">" -f 2 > diff_tmp_${FILE2}
grep "|" diff.txt | cut -d "|" -f 2 >> diff_tmp_${FILE2}
sed -e 's/^  //' diff_tmp_${FILE2} > diff_only_${FILE2}

# 作業ファイルの削除
rm -rf sort_${FILE1}
rm -rf sort_${FILE2}
rm -rf diff_tmp_${FILE2}

exit 0
[root@tanyao ~]#


■テストデータ準備

[root@tanyao ~]# cat file1.txt
/var/log/httpd/
/var/log/httpd/error_log
/var/log/httpd/error_log-20110103
/var/log/httpd/access_log
/var/log/httpd/access_log-20110103
[root@tanyao ~]#

[root@tanyao ~]# cat file2.txt
/var/log/httpd/
/var/log/httpd/error_log
/var/log/httpd/error_log-20110104
/var/log/httpd/access_log
/var/log/httpd/access_log.1
/var/log/httpd/access_log-20110103
[root@tanyao ~]#


■使用例および結果確認

[root@tanyao ~]# ./sabun.sh
[root@tanyao ~]# cat diff_only_file1.txt
/var/log/httpd/error_log-20110103
[root@tanyao ~]# cat diff_only_file2.txt
/var/log/httpd/access_log.1
/var/log/httpd/error_log-20110104
[root@tanyao ~]# cat diff_same.txt
/var/log/httpd/                                 /var/log/httpd/
/var/log/httpd/access_log                       /var/log/httpd/access_log
/var/log/httpd/access_log-20110103              /var/log/httpd/access_log-20110103
/var/log/httpd/error_log                        /var/log/httpd/error_log
[root@tanyao ~]#


■diffオプション

-y, --side-by-side
        side-by-side 出力形式を用いる。

side-by-side 形式

ファイルは 2 列に表示され、間に溝 (gutter) が置かれる。溝には以下のマーカーのいずれか 1 つが含まれる。

' '    対応する行が共通である。つまり、両方の行が同一であるか、違いが --ignore オプションのいずれかによって無視された。
|      対応する行が異なる。両方とも完全か、両方とも不完全かである。
<      ファイルは異なり、1 番目のファイルにだけこの行が含まれている。
>      ファイルは異なり、2 番目のファイルにだけこの行が含まれている。
(      1 番目のファイルにだけこの行が含まれているが、違いは無視される。
)      2 番目のファイルにだけこの行が含まれているが、違いは無視される。


サイト継続にご協力ありがとうございます。こちらの情報が何かのお役にたてれば幸いです。m(_ _)m

2010年12月9日木曜日

SELinux無効化

■OSインストール後にSELinuxを無効にする
/etc/selinux/configをvi等で編集し SELINUX=disabled とした後rebootする。

[root@tanyao ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted
[root@tanyao ~]# 

[root@tanyao ~]# reboot

※ SELINUX=permissive の状態でidコマンドを実行すると下記のように表示される。
[root@tanyao ~]# id
uid=0(root) gid=0(root) 所属グループ=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh


■SELinuxの動作モード確認と切り替え方法
----------------------------------------------------
Enforcing:許可されていないアクセスを拒否する
Permissive:許可されていないアクセスがあった場合でもログを出力するだけで実際にはアクセス制御を行わない
Disabled:SELinux無効
----------------------------------------------------
[root@tanyao ~]# setenforce 0
[root@tanyao ~]# getenforce
Permissive
[root@tanyao ~]# setenforce 1
[root@tanyao ~]# getenforce
Enforcing

※ /etc/selinux/config で SELINUX=permissive または SELINUX=enforcing となっている場合。
※ 上記コマンドは一時的な設定でありrebootすると/etc/selinux/configの設定に戻る。

⇒ /etc/selinux/config が SELINUX=disabled に設定されている場合で上記コマンドを実行すると下記のようになる。
[root@tanyao ~]# getenforce
Disabled
[root@tanyao ~]# setenforce 0
setenforce: SELinux is disabled
[root@tanyao ~]# setenforce 1
setenforce: SELinux is disabled

2010年11月13日土曜日

コマンドプロンプトでFTP

■FTPサーバ(Linux)に接続
c:\Temp>ftp 192.168.1.12
192.168.1.12 に接続しました。
220 (vsFTPd 2.0.5)
ユーザー (192.168.1.12:(none)): public
331 Please specify the password.
パスワード:
230 Login successful.

■対象ディレクトリに移動
ftp> cd /tmp/ftp
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
sample_data
226 Directory send OK.
ftp: 13 バイトが受信されました 0.00秒 13.00KB/秒。

■PCのディレクトリ移動
ftp> lcd put_data
ローカル ディレクトリは現在 c:\Temp\put_data です。

■バイナリーモードに変更
ftp> bin
200 Switching to Binary mode.

■確認時自動的にyesを選択させる
ftp> prompt
対話モード オフ。

■複数ファイルをアップロード
ftp> mput *
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 169506 バイトが送信されました 0.01秒 33901.20KB/秒。
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 183785 バイトが送信されました 0.01秒 36757.00KB/秒。
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 160099 バイトが送信されました 0.01秒 17788.78KB/秒。

■ファイルリストを表示
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
P1000713.JPG
P1000787.JPG
P1000952.JPG
sample_data
226 Directory send OK.
ftp: 55 バイトが受信されました 0.00秒 55.00KB/秒。

■サーバのディレクトリ移動
ftp> lcd ../get_data
ローカル ディレクトリは現在 c:\Temp\get_data です。

■サーバよりディレクトリごとダウンロード
ftp> mget sample_data
200 Switching to Binary mode.
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for sample_data/005.jpg (84702 bytes).
226 File send OK.
ftp: 84702 バイトが受信されました 0.01秒 8470.20KB/秒。
(中略)
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for sample_data/029.jpg (83463 bytes).
226 File send OK.
ftp: 83463 バイトが受信されました 0.01秒 11923.29KB/秒。

■切断
ftp> bye
221 Goodbye.

c:\Temp>

2010年11月12日金曜日

LinuxのシェルスクリプトでFTPする方法


LinuxのコマンドでFTPすることはもちろん可能ですが、
細かい内容はすぐ忘れてしまうのでシェルスクリプトにまとめてみました。

OS: CetnOS 5.7


■FTP用シェルスクリプトサンプル

#!/bin/sh

LOG="./log_ftp.log"
TARGET_HOST="192.168.216.232"
TARGET_USER="root"
TARGET_PASS="password"
TARGET_DIR="/tmp"
PUTDATA="putdata.txt"
GETDATA="getdata.txt"

uptime > $LOG

ftp -i -v -n $TARGET_HOST << END >> $LOG
user $TARGET_USER $TARGET_PASS
cd $TARGET_DIR
bin
# put $PUTDATA
# get $GETDATA
quit
END

uptime >> $LOG

exit 0

この情報が何かのお役にたてれば幸いです。m(_ _)m