Monthly Archives: 10月 2016

Linuxでawkを使って文字列を連結する方法

1.文字列-数字
[kduser@st24 ~]$ awk ‘BEGIN{a=”87″;b=”10kduser10″;print (a+b+0);}’

2.数字-文字列
[kduser@st24 ~]$ awk ‘BEGIN{a=87;b=87;c=(a””b);print c}’

3.文字列を連結
[kduser@st24 ~]$ awk ‘BEGIN{a=”a”;b=”b”;c=(a””b);print c}’

Javaでexcelファイルを作成するサンプル

Javaコード:
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelOp{
public static void main(String[] args){
XSSFWorkbook workbook = new XSSFWorkbook();

workbook.createSheet(“tanhen”);

FileOutputStream out = null;
try{
out = new FileOutputStream(“precdf.xlsx”);
workbook.write(out);
}catch(IOException e){
System.out.println(e.toString());
}finally{
try {
out.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}

phpでMySQLにSQLファイルをインポートする

PHPコード
<?php
$host = “172.17.100.21”;
$user = “root”;
$pwd = “pwdStr!2#”;
$file_dir = dirname(__FILE__);
$file_name = “sehuinfo.sql”;
$data_base = “dcftkdb”;

$conn = mysql_connect($host,$user,$pwd);

mysql_select_db($data_base,$conn);
$get_sql_data = file_get_contents($file_name,$file_dir);
$explode = explode(“;”,$get_sql_data);
$cnt = count($explode);
for($i=0;$i<cnt ;$i++){

$sql = $explode[$i];

$result = mysql_query($sql);

if($result){
echo “インポート成功:”.$i.”レコード<br>”;
}
else
{
echo “インポート失敗:”.mysql_error();
}
}
?>

Struts2でServletContextを取得する方法

1. ServletActionContext
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport{

public String execute() throws Exception {

ServletContext context = ServletActionContext.getServletContext();

return SUCCESS;

}

}

2. ServletContextAware

import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction
extends ActionSupport implements ServletContextAware{

ServletContext context;

public String execute() throws Exception {

return SUCCESS;

}

public void setServletContext(ServletContext context) {
this.context = context;
}
}

「PHP」開発–XMLファイルに要素を追加する方法

phpコード
$doc = new DOMDocument();
$doc->load( ‘kdfc.xml’ );
$students = $doc->getElementsByTagName( “student” );
foreach($students as $stu){
$computer = $stu->getElementsByTagName( “computer” )->item(0)->nodeValue;
$math = $stu->getElementsByTagName( “math” )->item(0)->nodeValue;
$english = $stu->getElementsByTagName( “english” )->item(0)->nodeValue;
$total = $stu->getElementsByTagName( “total” )->item(0)->nodeValue;
if($total){
echo “ノードが既に存在しますから追加されません”;
continue;
}
$total = $computer+$math+$english;
$newNode = $doc->createElement(“total”,$total);
$stu->appendChild($newNode);
}
$result = $doc->saveXML(‘kdfc.xml’);

phpでTcpポートが利用できるかどうかをチェックする

PHPコード
class TcpPortCheck {
public static $status;
public function __construct()
{
}
public function check($ip, $port){
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
socket_connect($sock,$ip, $port);
socket_set_block($sock);
self::$status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5);
return(self::$status);
}
public function checklist($lst){
}
public function status(){
switch(self::$status)
{
case 2:
echo “ポートが閉まっている\n”;
break;
case 1:
echo “ポートを利用できる\n”;
break;
case 0:
echo “タイムアウト\n”;
break;
}
}
}
$ip=’172.17.10.101′;
$port=80;
$tcpPort = new TcpPortCheck();
$tcpPort->check($ip, $port);
$tcpPort->status();

PHP開発-ランダムな文字列を作成する方法

方法1
function randStrGet($len)
{
$chars=”ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-=/?~!@#$%^&*()”; // characters to build the password from
$strbak=””;
for(;$len >= 1;$len–)
{
$position=rand()%strlen($chars);
$strbak.=substr($chars,$position,1);
}
return $strbak;
}

方法2
function generatePasswd( $length = 8 ) {
$chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}~`+=,.;:/?’;
$pwdinfo= ”;
while (strlen($pwdinfo) < $length)
{
$pwdinfo .= $chars[ mt_rand(0, strlen($chars) – 1) ];
}
return $pwdinfo;
}

方法3

function genRandomString($len)
{
$chars = array(
“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”,
“l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”,
“w”, “x”, “y”, “z”, “A”, “B”, “C”, “D”, “E”, “F”, “G”,
“H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”,
“S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “0”, “1”, “2”,
“3”, “4”, “5”, “6”, “7”, “8”, “9”, “!”, “@”, “#”, “$”,
“%”, “^”, “&”, “*”, “(“, “)”, “-“, “_”, “[“, “]”, “{“,
“}”, “~”, “`”, “+”, “=”, “,”, “.”, “:”, “;”, “/”, “?”
);
$charsLen = count($chars) – 1;
shuffle($chars);
$output = “”;
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}

echo “method1:”.randStrGet(30).”<br />”;
echo “method2:”.generatePasswd(30).”<br />”;
echo “method3:”.genRandomString(30).”<br />”;

MySQLストアドプロシージャWHILE、REPEAT、LOOPの使い方

1.while
mysql> create procedure prodemo10()
-> begin
-> declare i int;
-> set i=0;
-> while i<8 do
-> insert into t1(filed) values(i);
-> set i=i+1;
-> end while;
-> end;
mysql> delete from t1
mysql> call prodemo10()
mysql> select * from t1

2.REPEAT……END REPEAT
mysql> create procedure prodemo11()
-> begin
-> declare i int default 0;
-> repeat
-> insert into t1(filed) values(i);
-> set i=i+1;
-> until i>=8
-> end repeat;
-> end;
mysql> delete from t1
mysql> call prodemo11()

3.LOOP……END LOOP
mysql> create procedure prodemo12()
-> begin
-> declare i int default 0;
-> loop_label: loop
-> insert into t1(filed) values(i);
-> set i=i+1;
-> if i>=8 then
-> leave loop_label;
-> end if;
-> end loop;
-> end;
mysql> delete from t1
mysql> call prodemo12
mysql> select * from t1

4.Labels…END Labels
mysql> create procedure prodemo13()
-> label_1:begin
-> label_2:while 0=1 do leave label_2;end while;
-> label_3:repeat leave label_3;until 0=0 end repeat;
-> label_4:loop leave label_4;end loop;
-> end;
mysql> create procedure pro14()
-> label_1:begin
-> label_2:while 0=1 do leave label_2;end while label_2;
-> label_3:repeat leave label_3;until 0=0 end repeat label_3;
-> label_4:loop leave label_4;end loop label_4;
-> end label_1;
5.iterate
mysql> create procedure prodemo15()
-> begin
-> declare i int default 0;
-> loop_label:loop
-> if i=3 then
-> set i=i+1;
-> iterate loop_label;
-> end if;
-> insert into t1(filed) values(i);
-> set i=i+1;
-> if i>=8 then
-> leave loop_label;
-> end if;
-> end loop;
-> end;
mysql> delete from t1
mysql> call prodemo15
mysql> select * from t1

MySQLでlower(), uppper() の使い方

操作コマンド
mysql> select lower(‘EEE’);
+————–+
| lower(‘EEE’) |
+————–+
| eee |
+————–+
mysql> select upper(‘eee’);
+————–+
|upper(‘eee’) |
+————–+
| EEE |
+————–+
mysql> select lcase(‘EEE’);
+————–+
| lcase(‘EEE’) |
+————–+
| eee |
+————–+
mysql> select ucase(‘eee’);
+————–+
| ucase(‘eee’) |
+————–+
| EEE |

Linuxでnginx1.2.9インストール、起動停止方法と./configure項目整理

1.pcreのインストール
ダウンロード
http://www.pcre.org/
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz

インストール手順
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
# tar zxvf pcre-8.21.tar.gz
# cd pcre-8.21
# ./configure
# make
# sudo make install

gccの確認
[demoserver@dev Server]$ rpm -q gcc
gcc-4.1.2-51.el5

2.nginxのインストール
http://nginx.org/en/download.html
[demoserver@dev opt]tar zxvf nginx-1.2.9.tar.gz
[demoserver@dev opt] cd nginx-1.2.9
[demoserver@dev nginx-1.2.9]$ ./configure –with-http_stub_status_module
[demoserver@dev nginx-1.2.9]$ make install
[demoserver@dev nginx-1.2.9]$ sudo make install

3.nginx起動
[demoserver@dev nginx-1.3.12]$ cd /usr/local/nginx/sbin
[demoserver@dev sbin]$ sudo ./nginx &

4.nginx停止
[demoserver@dev sbin]$ sudo ./nginx -s stop
[demoserver@dev sbin]$ sudo ./nginx -s quit
Nginx -s stop (quick exit)
Nginx -s quit (graceful exit)
Nginx -s reload (changing configuration,start a new worker,quitting an old worker gracefully.)
Nginx -s reopen (reopenging log files)

5../configure
–prefix=<path> – /usr/local/nginx。
–sbin-path=<path> – <prefix>/sbin/nginx。
–conf-path=<path> – <prefix>/conf/nginx.conf。
–pid-path=<path> – <prefix>/logs/nginx.pid。
–lock-path=<path> – nginx.lockのパス
–error-log-path=<path> – <prefix>/logs/error.log。
–http-log-path=<path> – <prefix>/logs/access.log。
–user=<user> – nobody。
–group=<group> – nobody。
–builddir=DIR –
–with-rtsig_module – rtsig
–with-select_module –without-select_module – Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
–with-poll_module –without-poll_module – Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
–with-http_ssl_module – Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
–with-http_realip_module – ngx_http_realip_module
–with-http_addition_module – ngx_http_addition_module
–with-http_sub_module – ngx_http_sub_module
–with-http_dav_module – ngx_http_dav_module
–with-http_flv_module – ngx_http_flv_module
–with-http_stub_status_module – “server status” 页
–without-http_charset_module – ngx_http_charset_module
–without-http_gzip_module – ngx_http_gzip_module 。
–without-http_ssi_module – ngx_http_ssi_module
–without-http_userid_module – ngx_http_userid_module
–without-http_access_module – ngx_http_access_module
–without-http_auth_basic_module – ngx_http_auth_basic_module
–without-http_autoindex_module – ngx_http_autoindex_module
–without-http_geo_module – ngx_http_geo_module
–without-http_map_module -ngx_http_map_module
–without-http_referer_module – ngx_http_referer_module
–without-http_rewrite_module – ngx_http_rewrite_module.
–without-http_proxy_module – ngx_http_proxy_module
–without-http_fastcgi_module – ngx_http_fastcgi_module
–without-http_memcached_module – ngx_http_memcached_module
–without-http_limit_zone_module – ngx_http_limit_zone_module
–without-http_empty_gif_module – ngx_http_empty_gif_module
–without-http_browser_module – ngx_http_browser_module
–without-http_upstream_ip_hash_module – ngx_http_upstream_ip_hash_module
–with-http_perl_module – ngx_http_perl_module
–with-perl_modules_path=PATH – perlのパス
–with-perl=PATH – perl実行ファイルのパス
–http-log-path=PATH – Set path to the http access log
–http-client-body-temp-path=PATH – Set path to the http client request body temporary files
–http-proxy-temp-path=PATH – Set path to the http proxy temporary files
–http-fastcgi-temp-path=PATH – Set path to the http fastcgi temporary files
–without-http -HTTP server
–with-mail – IMAP4/POP3/SMTP
–with-mail_ssl_module – ngx_mail_ssl_module
–with-cc=PATH –
–with-cpp=PATH –
–with-cc-opt=OPTIONS – Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-cc-opt=”-I /usr/local/include”. If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: –with-cc-opt=”-D FD_SETSIZE=2048″.
–with-ld-opt=OPTIONS – Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate –with-ld-opt=”-L /usr/local/lib”.
–with-cpu-opt=CPU – pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
–without-pcre – PCRE
–with-pcre=DIR –
–with-pcre-opt=OPTIONS – Set additional options for PCRE building.
–with-md5=DIR – Set path to md5 library sources.
–with-md5-opt=OPTIONS – Set additional options for md5 building.
–with-md5-asm – Use md5 assembler sources.
–with-sha1=DIR – Set path to sha1 library sources.
–with-sha1-opt=OPTIONS – Set additional options for sha1 building.
–with-sha1-asm – Use sha1 assembler sources.
–with-zlib=DIR – Set path to zlib library sources.
–with-zlib-opt=OPTIONS – Set additional options for zlib building.
–with-zlib-asm=CPU – Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
–with-openssl=DIR – Set path to OpenSSL library sources
–with-openssl-opt=OPTIONS – Set additional options for OpenSSL building
–with-debug –
–add-module=PATH – Add in a third-party module found in directory PATH

6.nginxアンインストール
/usr/local/nginxを削除

1 / 3123