読者です 読者をやめる 読者になる 読者になる

カメニッキ

カメとインコと釣りの人です

最近便利だと思ってよく使うコマンドとか

すぐ忘れるので備忘録

コマンドの実行結果を一時ファイルを作成せずにdiffる

[root@v8794 nginx]# diff -u nginx.conf <(sed 's/kame/hoge/g' nginx.conf)
--- nginx.conf    2016-01-17 18:04:41.000000000 +0900
+++ /dev/fd/63    2016-01-23 00:43:56.601767533 +0900
@@ -9,17 +9,17 @@
     keepalive_timeout   65;
     server {
         listen       80;
-        server_name  kame.photos;
+        server_name  hoge.photos;
         location / {
-            root   /var/www/kame;
+            root   /var/www/hoge;
             index  index.php;
         }

         location ~ \.php$ {
-            root /var/www/kame;
+            root /var/www/hoge;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
-            fastcgi_param  SCRIPT_FILENAME  /var/www/kame$fastcgi_script_name;
+            fastcgi_param  SCRIPT_FILENAME  /var/www/hoge$fastcgi_script_name;
             include        fastcgi_params;
         }

@@ -31,19 +31,19 @@
         ssl_protocols TLSv1.2;
         #ssl_session_timeout  5m;

-        ssl_certificate      /etc/letsencrypt/live/kame.photos/fullchain.pem;
-        ssl_certificate_key  /etc/letsencrypt/live/kame.photos/privkey.pem;
+        ssl_certificate      /etc/letsencrypt/live/hoge.photos/fullchain.pem;
+        ssl_certificate_key  /etc/letsencrypt/live/hoge.photos/privkey.pem;

         location / {
-            root    /var/www/kame;
+            root    /var/www/hoge;
             index   index.php;
         }

    location ~ \.php$ {
-            root /var/www/kame;
+            root /var/www/hoge;
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
-            fastcgi_param  SCRIPT_FILENAME  /var/www/kame$fastcgi_script_name;
+            fastcgi_param  SCRIPT_FILENAME  /var/www/hoge$fastcgi_script_name;
             include        fastcgi_params;
        }

<( command ) の結果に /dev/fd/63 とファイルディスクリプタが割り当てられている

↑を応用して、find&replaceの確認

たとえば、あるディレクトリ以下のphpでおわるファイルの中身を wp -> KAME に変更する際に、想定のファイルが想定の部分だけ変更されるか、をチェックする。

[root@v8794 widgets]# for x in `find . -type f`; do diff -u $x <(sed 's/wp/KAME/g' $x); done
--- ./class-wp-widget-calendar.php  2015-09-27 10:07:25.000000000 +0900
+++ /dev/fd/63 2016-01-23 01:24:20.132003630 +0900
@@ -48,7 +48,7 @@
     * @param array $instance The settings for the particular instance of the widget.
     */
    public function widget( $args, $instance ) {
-       /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+       /** This filter is documented in KAME-includes/widgets/class-KAME-widget-pages.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        echo $args['before_widget'];
@@ -94,7 +94,7 @@
     * @param array $instance Current settings.
     */
    public function form( $instance ) {
-       $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
+       $instance = KAME_parse_args( (array) $instance, array( 'title' => '' ) );
        $title = sanitize_text_field( $instance['title'] );
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
--- ./class-wp-nav-menu-widget.php  2015-09-29 00:31:25.000000000 +0900
+++ /dev/fd/63 2016-01-23 01:24:20.134003626 +0900
@@ -39,12 +39,12 @@
     */
    public function widget( $args, $instance ) {
        // Get menu
-       $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
+       $nav_menu = ! empty( $instance['nav_menu'] ) ? KAME_get_nav_menu_object( $instance['nav_menu'] ) : false;

        if ( !$nav_menu )
            return;

-       /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
+       /** This filter is documented in KAME-includes/widgets/class-KAME-widget-pages.php */
        $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        echo $args['before_widget'];
@@ -64,7 +64,7 @@
         * @since 4.4.0 Added the `$instance` parameter.
         *
         * @param array    $nav_menu_args {
-        *     An array of arguments passed to wp_nav_menu() to retrieve a custom menu.
+        *     An array of arguments passed to KAME_nav_menu() to retrieve a custom menu.
         *
         *     @type callable|bool $fallback_cb Callback to fire if the menu doesn't exist. Default empty.
       *     @type mixed         $menu        Menu ID, slug, or name.
@@ -73,7 +73,7 @@
       * @param array    $args          Display arguments for the current widget.
       * @param array    $instance      Array of settings for the current widget.
       */
-     wp_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );
+     KAME_nav_menu( apply_filters( 'widget_nav_menu_args', $nav_menu_args, $nav_menu, $args, $instance ) );

      echo $args['after_widget'];

同じパターンのファイルを大量に生成する

cat << EOF > xxx.txtcat << EOF | command1 | command2 が、一時ファイルを用意せず色々やれてすき

[root@v8794 sample]# for x in `seq 1 100`; do cat << EOF | sed "s/_PLACEHOLDER_/for_${x}_replace/g" > work/${x}.txt
<html>
  <head>_PLACEHOLDER_</head>
  <body>
    _PLACEHOLDER_XXXXXXXXXXXXXXXXXXXXX
  </body>
</html>

EOF
; done

[root@v8794 sample]# ls -lt work/
合計 400
-rw-r--r-- 1 root root 109  123 01:02 2016 1.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 10.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 100.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 11.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 12.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 13.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 14.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 15.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 16.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 17.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 18.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 19.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 2.txt
-rw-r--r-- 1 root root 109  123 01:02 2016 20.txt

[root@v8794 sample]# cat work/1.txt
<html>
  <head>for_1_replace</head>
  <body>
    for_1_replaceXXXXXXXXXXXXXXXXXXXXX
  </body>
</html>

予め定められたリストにマッチした場合に、規定値を使用して処理する

#! /bin/sh

DATA="
01  AAAAAAAAAAAAAAAAA tanaka
02  BBBBBBBBBBBBBBBBB ikeda
03  CCCCCCCCCCCCCCCCC yamada
"

d=$(echo "${DATA}" | grep $1)

read no txt user <<< "${d}"

echo $no
echo $txt
echo $user

量が多くなるのであればDBなど使用するほうがよいが、小さなプログラムに便利。

read x y z <<< $val もすき。