Monthly Archives: 2月 2015

bashで始めるシェルスクリプトifとthenを利用するサンプルコード

例1 数字の判断
#!/bin/bash
read -p “数字を入力してください:” a
if [ $a -lt 200 ]
then
echo “a<200” else echo “a>=200”
fi

例2 現在のディレクトリにファイルが存在するかどうかを判断

#!/bin/bash
read -p “ファイル名を入力してください:” name
if [ -e $name ]
then
echo -n “$name存在する,”
if [ -d $name ]
then
echo “ディレクトリである”
else
echo “同じディレクトリではない”
fi
else
echo “$name存在しない”
fi

例3 現在の環境をコマンドにインストールされているかを確認
#!/bin/bash
read -p “コマンドを入力してください:” var
if which $var>/dev/null
then
echo “$var存在”
else
echo “$var存在しない”
fi

PHPでregister_shutdown_function()を利用するサンプルコード

サンプルコード:
$start_time = microtime(true);
register_shutdown_function(‘my_shutdown’);

// do some stuff
// …

function my_shutdown() {
global $start_time;

echo “execution took: “.
(microtime(true) – $start_time).
” seconds.”;
}

シェルスクリプト実行5秒自動的終了コード

参考コード:

#!/bin/bash

#name show_status.sh

#echo $$ > /var/run/show_status.log
#sleep 5 && kill cat /var/run/show_status.log &

#start code
echo “welcome to arkgame.com”
#end code

Linuxプログラムのプロセス依存ライブラリを確認する方法

1.lddで依存ライブラリを確認
[root@~]# ldd /usr/local/php/bin/php
linux-vdso.so.1 => (0x00007fff753f5000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00000031f4600000)
librt.so.1 => /lib64/librt.so.1 (0x000000311ee00000)
libmysqlclient.so.18 => /usr/local/mysql/lib/libmysqlclient.so.18 (0x00007f767b422000)
libmcrypt.so.4 => /usr/lib64/libmcrypt.so.4 (0x00007f767b1ef000)
libiconv.so.2 => /usr/local/lib/libiconv.so.2 (0x00007f767af0a000)
libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x0000003519800000)
libm.so.6 => /lib64/libm.so.6 (0x00000031f2200000)
libdl.so.2 => /lib64/libdl.so.2 (0x00000031f1600000)
libnsl.so.1 => /lib64/libnsl.so.1 (0x00000031f3a00000)
libxml2.so.2 => /usr/lib64/libxml2.so.2 (0x00000031f8a00000)
libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f767acf2000)
libssl.so.10 => /usr/lib64/libssl.so.10 (0x000000311c600000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00000031f7600000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x000000311ce00000)
libc.so.6 => /lib64/libc.so.6 (0x00000031f0e00000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00000031f3200000)
libfreebl3.so => /lib64/libfreebl3.so (0x00000031f4e00000)
/lib64/ld-linux-x86-64.so.2 (0x00000031f0a00000)
libidn.so.11 => /lib64/libidn.so.11 (0x00000031f7e00000)
libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x000000351c000000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x000000311f200000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x000000311e200000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00000031f7200000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x000000311e600000)
libssl3.so => /usr/lib64/libssl3.so (0x000000351b800000)
libsmime3.so => /usr/lib64/libsmime3.so (0x000000351bc00000)
libnss3.so => /usr/lib64/libnss3.so (0x000000351a800000)
libnssutil3.so => /usr/lib64/libnssutil3.so (0x000000351a400000)
libplds4.so => /lib64/libplds4.so (0x000000351ac00000)
libplc4.so => /lib64/libplc4.so (0x000000351b400000)
libnspr4.so => /lib64/libnspr4.so (0x000000351b000000)
libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x0000003519c00000)
liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x000000351a000000)
libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00000031f8200000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00000031f7a00000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00000031f6e00000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00000031f2600000)

2.pmapで未知プログラムの依存ライブラリを確認
[root@~]# objdump -p /usr/local/php/bin/php |grep NEEDED
NEEDED libcrypt.so.1
NEEDED librt.so.1
NEEDED libmysqlclient.so.18
NEEDED libmcrypt.so.4
NEEDED libiconv.so.2
NEEDED libcurl.so.4
NEEDED libm.so.6
NEEDED libdl.so.2
NEEDED libnsl.so.1
NEEDED libxml2.so.2
NEEDED libz.so.1
NEEDED libssl.so.10
NEEDED libcrypto.so.10
NEEDED libpthread.so.0
NEEDED libc.so.6
NEEDED libresolv.so.2

3.pmapで実行中プログラムのプロセスを確認
[root@~/software/pldd]# ps -ef|grep php-fpm
root 26534 1 0 2014 ?00:01:34 php-fpm: master process (/usr/local/php-5.3.29/etc/php-fpm.conf)
nobody 26535 26534 0 2014 ?00:00:04 php-fpm: pool www
nobody 26536 26534 0 2014 ?00:00:05 php-fpm: pool www
root 30510 30324 0 00:39 pts/000:00:00 grep php-fpm
[root@~/software/pldd]# pmap 26534 |head
26534: php-fpm: master process (/usr/local/php-5.3.29/etc/php-fpm.conf)
0000000000400000 10452K r-x– /usr/local/php-5.3.29/sbin/php-fpm
0000000001035000 76K rw— /usr/local/php-5.3.29/sbin/php-fpm
0000000001048000104K rw—[ anon ]
0000000002a65000 3448K rw—[ anon ]
000000311c600000388K r-x– /usr/lib64/libssl.so.1.0.1e
000000311c661000 2048K —– /usr/lib64/libssl.so.1.0.1e
000000311c861000 16K r—- /usr/lib64/libssl.so.1.0.1e
000000311c865000 28K rw— /usr/lib64/libssl.so.1.0.1e
000000311ce00000 92K r-x– /lib64/libpthread-2.12.so

「Java学習」EditTextで最大入力文字数を制限する

参考コード:

public class EditTextWithLimitChar extends EditText {
private int NUM_MAX_CHAR = Integer.MAX_VALUE;
private int sum = 0; // 文字列合計
private CharSequence temp;
private int editStart;
private int editEnd;
/**
* 文字数の上限を設定
*
* @param limits
*/
public void setLimits(int limits) {
NUM_MAX_CHAR = limits;
}
public EditTextWithLimitChar(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public EditTextWithLimitChar(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public EditTextWithLimitChar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init() {
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
temp = s;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
editStart = getSelectionStart();
editEnd = getSelectionEnd();
Log.d(“TAG”, “editStart=” + editStart + “, ” + “editEnd=”
+ editEnd);
if (temp.length() > NUM_MAX_CHAR) {
Toast.makeText(getContext(), “文字数は制限を超える”,
Toast.LENGTH_SHORT).show();
s.delete(editStart – 1, editEnd);
int tempSelection = editStart;
setText(s);
setSelection(tempSelection);
}
}
});
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}

JavaScriptで要素を削除するには実装プログラム

参考コード

<html>
<head>
<title>Javascriptで要素を削除</title>
<script type=”text/javascript”><!–

function $(nodeId)
{
return document.getElementById(nodeId);
}
function $Name(tagName)
{
return document.getElementsByTagName(tagName);
}
//メッセージを交換
function replaceMsg()
{
var newNode = document.createElement(“P”);//Pタグを作成
newNode.innerHTML = “<font color=’red’>added element</font>”;
var oldNode = $Name(“P”)[0];//bodyのp要素を取得
oldNode.parentNode.replaceChild(newNode,oldNode);
}
//メッセージを削除
function removeMsg()
{
var node = $(“p2”);
var nodeBtn = $(“remove”);
//node.parentNode.removeChild(node);
document.body.removeChild(node);
//nodeBtn.parentNode.removeChild(nodeBtn);//削除ボタン
document.body.removeChild(nodeBtn);
//document.body.removeNode();
}

function addbefore()
{
var newNode = document.createElement(“p”);
//var newText = document.createTextNode(“before add element”);
//newNode.appendChild(newText);
newNode.innerHTML = “<font color=’red’>before add element</font>”;
var oldNode = $(“p3”);
//document.body.insertBefore(newNode,oldNode);
oldNode.parentNode.insertBefore(newNode,oldNode);
}

function addlast()
{
var newNode = document.createElement(“p”);//Pタグの作成
//var newText = document.createTextNode(“last element add”);
//newNode.appendChild(newText);
newNode.innerHTML = “<font color=’red’>last add element</font>”;
var oldNode = $(“p3”);

oldNode.appendChild(newNode);
//document.body.appendChild(newNode);
}

window.onload = function addArrayMsg()
{
var arrayMsg = [‘one’,’two’,’three’,’four’,’five’];//配列を作成
var fragment = document.createDocumentFragment();//ドキュメントフラグメントを作成
var newNode ;
for (var i=0 ;i<arrayMsg.length ;i++ )
{
newNode = document.createElement(“P”);
var nodeText = document.createTextNode(arrayMsg[i]);
newNode.appendChild(nodeText);//
fragment.appendChild(newNode);
}
document.body.appendChild(fragment);
}
//行を追加
function addRow()
{
var tab = $(“myTable”);
var oldTr = $(“handleTr”);
var newTr = tab.insertRow();//行の作成
var newTd1 = newTr.insertCell();//セルの作成
var newTd2 = newTr.insertCell();
newTd1.innerHTML = “<input type=’checkbox’ />”;
newTd2.innerHTML = “<input type=’text’ />”;

}
//行を削除
function removeRow()
{
var tab = $(“myTable”);
// if(tab.rows.length>0){
// tab.deleteRow();
// if(tab.rows.length==1)
// tab.deleteCaption();
// }

var cbbox ;
for(var i=0;i<tab.rows.length;i++){
cbbox = tab.rows[i].childNodes[0].childNodes[0];//inputを取得
if(cbbox.checked){

tab.deleteRow(i–);
}
}
}

//全部選択
function selAll(value){
var items = document.all.tags(“input”);//ページの全てinputを取得
for(var i = 0;i<items.length;i++){
if(items[i].type==”checkbox”){//checkboxの判断
items[i].checked = value.checked;
}
}
}
//input要素の値を取得
function getInputValue()
{
var inputArray = new Array();//配列を作成
var tab = $(“myTable”);
var tbInput;
for(var i=0;i<tab.rows.length;i++){
tbInput = tab.rows[i].childNodes[1].childNodes[0].value;
if(tbInput!=””&&tbInput!=null)
inputArray.push(tbInput);
}
inputArray = inputArray.join(“*^&”);
$(“showValue”).value = inputArray;
}
var x =’10+20′;
(“alert(x);”)
// –></script>
</head>
<body>
<p id=”p1″>Hello World!</p>
<input type=”button” value=”change” onclick=”replaceMsg();” />
<p id=”p2″>Are you sure delete?</p>
<input type=”button” id=”remove” value=”delete” onclick=”removeMsg();” />
<p id=”p3″>add element</p>
<input type=”button” id=”addbefore” value=”add befroe” onclick=”addbefore();” />
<input type=”button” id=”addlast” value=”add last” onclick=”addlast();” />
<p></p>
<div style=”border:1px solid blue;height:300px”>
<table id=”myTable” cellpadding=”0″ cellspacing=”0″ border=”1px solid blue” style=”padding:4px;” style=”padding:4px;”>
</table>
<input type=”checkbox” onclick=”selAll(this);” />
<input type=”button” value=”add Row” id=”addRow” onclick=”addRow();” />
<input type=”button” value=”remove Row” id=”removeRow” onclick=”removeRow();” />
<textarea rows=”5″ cols=”25″ id=”showValue” />

JavaScriptのイベントハンドラ処理プログラム(イベントリスナー)

参考コード:
<html>
<head>
<title>event util test</title>
<script type=”text/javascript”>
var eventUtil = {
addListener: function(element, type, hander) {
if (element.addEventListener) {
element.addEventListener(type, hander, false);
} else if (element.attachEvent) {
element.attachEvent(‘on’ + type, hander);
} else {
element[‘on’ + type] = hander;
}
},
getEvent: function(event) {
return event || window.event;
//return event ? event : window.event;
},
getTarget: function(event) {
return event.target || event.srcElement;
},
preventDefault: function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
},
removeListener: function(element, type, hander) {
if (element.removeEventListener) {
element.removeEventListener(type, hander, false);
} else if (element.deattachEvent) {
element.detachEvent(type, hander);
} else {
element[‘on’ + type] = null;
}
},
stopPropagation: function(event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
};
</script>
</head>
<body>
<input type=”button” value=”クリックしてください” id=”btn” />
<a href=”http://www.arkgame.com”>arkgame.com</a>
<script type=”text/javascript”>
(function() {
var btn = document.getElementById(“btn”);
var link = document.getElementsByTagName(“a”)[0];
eventUtil.addListener(link, “click”, function(event) {
alert(“prevent default event”);
var event = eventUtil.getEvent(event);
eventUtil.preventDefault(event);
});
eventUtil.addListener(btn, “click”, function(event) {
var event = eventUtil.getEvent(event);
var target = eventUtil.getTarget(event);
alert(event.type);
alert(target);
eventUtil.stopPropagation(event);
});
eventUtil.addListener(document.body, “click”, function() {
alert(“click body”);
});
})();
</script>
</body>
</html>

JavaScriptのイベントハンドラ一覧にeventの使い方

1.eventパラメータはundefinedまたはnullを判断
if (!event) event=window.event;

2.ターゲット要素の引用
var target=(event.target) ? event.target : event.srcElement;

「javascript学習」extendを実現するソースコード

サンプルコード:
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if ( typeof target === “boolean” ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== “object” && !jQuery.isFunction(target) ) {
target = {};
}
// extend jQuery itself if only one argument is passed
if ( length === i ) {
target = this;
–i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we’re merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don’t bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};

7 / 7« 先頭...34567