2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプト・PowerShellのメモ

Last updated at Posted at 2015-06-26

■Node.jsがすでにインストール済みか確認、入っていなければダウンロード

# !/bin/sh
node -v &> /dev/null
if [ $? -ne 0 ]; then
  curl -O https://nodejs.org/dist/v0.12.5/node-v0.12.5.pkg
fi

■ コマンド &

コマンドをバックグラウンドで実行する
●参考URL
http://itpro.nikkeibp.co.jp/article/COLUMN/20060224/230589/

■ /dev/null

ゴミ箱を指す
●参考URL
http://www.hangout.co.jp/blog/archives/168

■ -ne

等しくないを表す、おそらく not equel の略
●参考URL
http://shellscript.sunone.me/if_and_test.html

■コマンドの実行結果を変数に設定する

 VAR=`command`

 計算結果、文字列の編集結果、等を変数に設定したい場合は、「``」(バッククォート)を使用する。

 Macのキーボードでバッククオートの入力
 Shift+@

■カレントディレクトコマンドの実行結果を変数に入れて、表示

 PathData=`pwd`
 echo $PathData 

■ファイルやディレクトリの存在をチェックする方法

例)指定パスにフォルダがなければ権限付き作成

Current=`pwd`
NodePath=/NodeJs
CheckPath=$Current$NodePath
if [ ! -e $CheckPath ]; then
	mkdir -m 777 NodeJs
fi

例)指定ファイルがなければ権限付き作成

JsFileName=LocalServer.js
if [ ! -e $JsFileName ]; then
	touch $JsFileName
	chmod 777 $JsFileName
fi

●参考URL
http://sweng.web.fc2.com/ja/program/bash/bash-check-file.html

■(Linux)シェルスクリプトで変数に改行を含めたい

JsString="var connect = require('connect'),
    	serveStatic = require('serve-static');
	var app = connect();
	app.use(serveStatic(__dirname));
	app.listen(5000);"

	echo "$JsString" > $JsFileName

●参考URL
http://jehupc.exblog.jp/15728862/

■PowerShellのメモ

■powerShellから他の処理を実行して終了待ち

Start-Process -FilePath "C:\Program Files (x86)\teraterm\ttpmacro.exe" -ArgumentList "C:\work.ttl" -Wait

●参考URL
http://harikofu.blog.fc2.com/blog-entry-28.html

●Windows管理者のためのPowerShell
http://powershell.wiki.fc2.com/wiki/パスの操作

■ダウンロード完了を待ってインストールファイルを実行

下記がやってることは、指定URLのファイルをダウンロードし、指定した名前で保存 --> 保存したファイルを実行

Invoke-WebRequest -Uri https://nodejs.org/dist/v0.12.5/x64/node-v0.12.5-x64.msi -OutFile ./node.msi
./node.msi -ms

■指定フォルダが存在するか確認し、無ければ作成

下記がやっている事は NodeJsフォルダが存在するか確認し、無ければ NodeJsフォルダを作成

$NodeJsPath = "NodeJs"
if(-not(Test-Path $NodeJsPath))
{
	echo Not Found
	# カレント・ディレクトリにNodeJsという名前のフォルダを作成する
	New-Item $NodeJsPath -type Directory
}
else
{
	echo Found
}

■指定ファイルに指定文字列を書き込み

$JsFileName="LocalServer.js"

# JSファイルのソースコードを文字列にする
$JsString="var connect = require('connect'),
serveStatic = require('serve-static');
		var app = connect();
		app.use(serveStatic(__dirname));
		app.listen(5000);"

# ファイルに書き込む
Set-Content $JsFileName $JsString
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?