はじめに
ニッチなお話ですが…
意外と苦戦したので、メモがてら記事にします。
「\n」に置換する理由は、その後MySQLで使う予定があるからです。
結論
str = str.replace(/\n/g, "\\n");
セル内改行は、「\n」で検索できました。
変換後は、「\」をエスケープしてやる必要があるみたいです。
以上でした!
小ネタです。
TeraTermはCygwinへの接続があらかじめ考えられており、深く考えずに接続可能だと思っていたのですが…
うまく行かずアレ?となったので、共有です。
TeraTerm → Cygwin の順番でインストールしていたことが原因でした。
TeraTerm → Cygwin → TeraTerm ともう一度インストールすることで、接続可能になりました。
ここに書いてある通りなのですが、
64bit Cygwin に接続するには 64 bit Cygwin環境 でビルドされた cygterm+ プログラムが必要です。通常はTera Termのインストーラが、インストールされているCygwinのバージョンを判定して適切な実行ファイルをコピーします。
ということで、これに従いインストーラをもう一回叩いたらうまくいったというお話です。
Cygterm+をビルドする
というビルド方法についても記載がありますが、こちらを始める前に試してみるといいと思います。
WSLの機能もよくなっておりますが…
結局はCygwinが便利で離れられません。
まだまだお世話になると思います。
以上でした。
今や大体のサーバーはUTF-8だと思うのですが、たまーにEUCだったりします。
そうなったときに、TeraTermなどターミナルエミュレータの文字コードを変更しないと、日本語が文字化けして困るときがあります。
たまになので面倒くさい…それをログインマクロで対応しようというお話です。
ちなみに、追加でプラグインをインストールしないといけません。
exeファイルを準備しましょう。
面倒でなければ、画面から設定しましょう。
「端末の設定」から、文字コードの変更が可能です。
「送信」に赤マルをつけ忘れましたが…
受信と送信の両方を、例えば「EUC」に変更しましょう。
もし、「UTF-8」以外に変更ができない!という方は…
「全般設定」の赤マルの部分が「UTF-8」になっている可能性があります。
「Japanese」に変更して再度挑戦してみましょう。
上の画像の、選択されたプラグイン、「TTXKanjiMenu」を選んでインストールします。
ポイントは下記マニュアルにもある、callmenuです。
ウィンドウのメニューを実行できます。
マクロですが、具体的には下記のようになりました。
;; 設定 USER_NAME = 'UserName' HOST_NAME = 'HostName' PASS_FILE = 'C:\path\to\password.dat' ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; パスワード取得 getpassword PASS_FILE USER_NAME password ;; sshコマンド作成 command = HOST_NAME strconcat command ':22 /ssh /auth=password /user=' strconcat command USER_NAME strconcat command ' /passwd=' strconcat command password ;; 接続 connect command ; EUC callmenu 54011 ; [KanjiCode] Recv: EUC-JP callmenu 54111 ; [KanjiCode] Send: EUC-JP ;; マクロ終了 end
TeraTermのマクロは便利ですね…これがあるので、私はTeraTermを使い続けております。
プラグインを使えば、メニューの項目をいじることができることもわかりました。
もっと別の、便利な使い方もできるとは思いますが、今回はここまで。
以上でした。
先日の記事では、VirtualBoxにCentOS7のイメージを入れて、Laravelの開発環境を準備しました。
しかし、Laravelの公式サイトでは「Homestead」という環境もあるよと教えてくれています。
これを今回は導入してみようと思います。
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.
Laravel Homestead(入植農地、「ホームステード」)はパッケージを事前に済ませた、Laravel公式の"box"で、PHPやWebサーバ、その他のサーバソフトウェアをローカルマシンにインストールする必要なく、素晴らしい開発環境を準備できます。
公式サイトの文言を借りるとこんな感じ。
Vagrantで提供している環境のひとつですね。
Before launching your Homestead environment, you must install VirtualBox, VMWare, Parallels or Hyper-V as well as Vagrant.
Homestead環境を起動する前にVagrantと共に、VirtualBoxか、VMWare、Parallels、Hyper-Vをインストールする必要があります。
Windows 10 Pro だとHyper-Vが使えるのですが、私のPCはHomeなので、VirtualBoxを使いました。
どのソフトを利用しても、Vagrantがラップしてくれる関係で、あまり意識することはないと思います。
ここのインストールについては省略します。
私の場合は、Windowsの64bit版をインストールしました。
msiファイルが229MBありました。
インストールするにあたって、基本的には「Next」で進めていくだけなので、ここも省略します。
ここからはコマンドでの操作になります。
Powershellでコマンド実行が可能ですが、私はいつも使っているCygwinで実行しました。
実行コマンドに差異はないはず。
公式のドキュメントにある通り、コマンドを実行していきます。
vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead' box: URL: https://vagrantcloud.com/laravel/homestead This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) hyperv 2) parallels 3) virtualbox 4) vmware_desktop Enter your choice: 3
プロバイダーと書いてありますが、ここで利用する仮想化ソフトを選択します。
私の場合はVirtualBoxなので、「3」です。
Enter your choice: 3 ==> box: Adding box 'laravel/homestead' (v7.1.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/7.1.0/providers/virtualbox.box box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com box: Progress: 100% (Rate: 2568k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'laravel/homestead' (v7.1.0) for 'virtualbox'!
「Successfully」とのことで無事成功のようです。
いよいよHomesteadのインストールです。
homeディレクトリにHomesteadディレクトリを作成することになります。
最初に言い忘れていましたが、Gitが必要になります。
$ git clone https://github.com/laravel/homestead.git ~/Homestead Cloning into '/home/${USER}/Homestead'... remote: Enumerating objects: 3314, done. remote: Total 3314 (delta 0), reused 0 (delta 0), pack-reused 3314 Receiving objects: 100% (3314/3314), 703.94 KiB | 1.02 MiB/s, done. Resolving deltas: 100% (2004/2004), done.
masterブランチは安定しているわけではないので、特定のブランチをcheckoutすることを推奨されています。
GitHubのサイトを見に行きましょう。
私の場合は最新の「v8.2.0」を指定しました。
$ cd Homestead/ ~/Homestead$ ~/Homestead$ git checkout -b v8.2.0
次に、Homestead.yamlという設定ファイルを生成するために、下記コマンドを実行します。
Mac/Linux と Windows とで違うところですね。
// Mac / Linux... bash init.sh // Windows... init.bat
私はLinux組なので、init.shを実行。
$ bash init.sh Homestead initialized!
設定ファイルの初期値は下記のとおりです。
$ cat Homestead.yaml --- ip: "192.168.10.10" memory: 2048 cpus: 1 provider: virtualbox authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa folders: - map: ~/code to: /home/vagrant/code sites: - map: homestead.test to: /home/vagrant/code/public databases: - homestead # ports: # - send: 50000 # to: 5000 # - send: 7777 # to: 777 # protocol: udp # blackfire: # - id: foo # token: bar # client-id: foo # client-token: bar # zray: # If you've already freely registered Z-Ray, you can place the token here. # - email: foo@bar.com # token: foo # Don't forget to ensure that you have 'zray: "true"' for your site.
慣れてきたらいろいろと変更が必要になると思いますが、まず動かしてみることを考えるのであれば、一旦このままでもいいと思います。
ただし、いくつかこの通りの設定になっているかどうかを確認しないといけませんが。
例えば。
provider: virtualbox
利用する仮想化ソフトは合っているか。
authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa
sites: - map: homestead.test to: /home/vagrant/code/public
http://homestead.test で仮想環境にアクセスするようにしているので、必要に応じてhostsファイルを書き換える必要があります。
Windowsの場合は"C:\Windows\System32\drivers\etc\hosts"にありますので、メモ帳を「管理者権限で実行」で実行した上で、ファイルを開いて下記を追加してください。
192.168.10.10 homestead.test
~/Homestead$ vagrant up
これで実行できます。あとはブラウザからアクセスしてみたいのですが…
http:// homestead.test http://192.168.10.10
↓
No input file specified.
と表示されるのではないでしょうか。
これは、Homestead.yaml に設定したドキュメントルートに、ファイルが存在しないために発生しています。
なので、例えば
# 仮想環境に接続 $ vagrant ssh vagrant@homestead:~$ mkdir -p /home/vagrant/code/public/ vagrant@homestead:~$ vim /home/vagrant/code/public/index.php <?php echo "Hello, Homestead!";
とディレクトリとファイルを作成してあげれば、
Hello, Homestead!
と、きちんと意図した動作をすることが確認できると思います。
どこからでも vagrant up コマンドを実行できるように、bash関数を作成しておくと便利です。
これもマニュアルにあることですが。
$ vim ~/.bash_profile function homestead() { ( cd ~/Homestead && vagrant $* ) }
Windowsの場合は、Homestead.bat ファイルを下記の中身で作成して、PATHにファイルの場所を指定すればOKとのこと。
@echo off set cwd=%cd% set homesteadVagrant=C:\Homestead cd /d %homesteadVagrant% && vagrant %* cd /d %cwd% set cwd= set homesteadVagrant=
こうすれば今後、例えば"homestead ssh"と打てば、どこのディレクトリにいても"vagrant ssh"が実行可能になります。
開発に必要なファイルの場所やコマンドなど。
先に登場させましたが、
vagrant ssh
で接続が可能です。
$ vagrant ssh Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64) _ _ _ | | | | | | | |__ ___ _ __ ___ ___ ___| |_ ___ __ _ __| | | '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` | | | | | (_) | | | | | | __/\__ \ || __/ (_| | (_| | |_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_| * Homestead 8.0.0 released! PHP 7.3 is now the default! * Settler v7.0.0 released! Make sure you update * Need PHP 5.6 or 7.0? Homestead 7.x Settler 6.4.0 0 packages can be updated. 0 updates are security updates. Last login: Sun Mar 10 13:48:36 2019 from 10.0.2.2 vagrant@homestead:~$
vagrant@homestead:~$ id uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant),4(adm),24(cdrom),27(sudo),30(dip),33(www-data),46(plugdev),108(lxd),113(lpadmin),114(sambashare)
vagrant@homestead:~$ ls -l /var/log/nginx/ total 4 -rw-r----- 1 www-data adm 0 Feb 4 23:16 access.log -rw-r----- 1 www-data adm 0 Feb 4 23:16 error.log -rw-r--r-- 1 root root 3586 Mar 10 13:11 homestead.test-error.log
上記の
No input file specified.
の件も、ここのログを見ると書いてあります。
vagrant@homestead:~$ mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> SELECT user, host FROM mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | homestead | % | | homestead | 0.0.0.0 | | root | 0.0.0.0 | | debian-sys-maint | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 7 rows in set (0.00 sec)
まだあると思うので、別途追記します。
まだ本当に下準備だけなので、これからLaravelプロジェクトを載せたりする必要があります。
とはいえ、上記の設定だけですでにここまでの準備が済むのはとっても便利です。
今後、積極的に使っていきたいと思います。
以上でした!
小ネタです。
GAS (Google Apps Script) で文字コードを指定しつつ、ファイル出力することを考えます。
/** * ファイル書き出し * @param {string} fileName ファイル名 * @param {string} content ファイルの内容 */ function createFile(fileName, content) { var folder = DriveApp.getFolderById('##### フォルダID #####'); var contentType = 'text/plain'; var charset = 'utf-8'; // Blob を作成する var blob = Utilities.newBlob('', contentType, fileName) .setDataFromString(content, charset); // ファイルに保存 folder.createFile(blob); }
フォルダID、ContentType、文字コードを指定するだけでOKです。
下記URLの******の部分がフォルダIDです。
https://drive.google.com/drive/u/0/folders/******
本当は createFile(name, content, mimeType)
というズバリなものもあるのですが、こちらは文字コードの指定ができません。
Class DriveApp | Apps Script | Google Developers
遠回りなようですが、Blobオブジェクトを生成して、setDataFromString(string, charset)
でファイルの内容を文字コード指定でセットしております。
Class Blob | Apps Script | Google Developers
JavaScriptの知識だけでは対応できないことも多いので、今後も小さいネタでも投下していこうと思います。
以上でした!