トップ «前の日(03-22) 最新 次の日(03-24)» 追記

Masa's blog

検索キーワード:

2011年03月23日 GNU java

_ GNU java

実行形式を作成&実行する方法。

$ gcj -Wall -o Hello --main=Hello Hello.java
$ ./Hello
Hello World
$
  • -o Hello で実行形式ファイルを指定。
  • --main=Hello で main()を含むクラス名を指定。

クラスファイルを作成&実行する方法。

$ gcj -C Hello.java
$ gij Hello
Hello World
$

2016年03月23日 My first PowerShell programming

_ My first PowerShell programming

000_hello.ps1

write-output "こんにちは、PowerShell!"

001_stdio.ps1

Write-Output "文字列1"

$output = "文字列1" + "文字列2"
Write-Output $output

# Read-Hostはキーボードからの入力。標準入力からの読み取りではない!
$input = Read-Host "入力1"
Write-Output $input

# Write-Hostは画面への出力。標準出力への書き出しではない!
Write-Host -NoNewline "入力2 -> "
$input = Read-Host
Write-Output $input

[Console]::Out.write("入力3:")
$input = [Console]::In.readLine()
[Console]::Out.writeLine($input)

$input = Read-Host -AsSecureString "パスワード"
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($input)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr);
Write-Output $password

002_variable.ps1

$a = "文字列1"
Write-Output $a
Write-Output "文字列は $a"
Write-Output '文字列は $a'

$a = "文字列1"
$b = "文字列2"
$c = '$c=' + $a + $b
Write-Output $c

003_array.ps1

$array1 = @("項目1", "項目2", "項目3")

Write-Output $array1[0]
Write-Output $array1[1]
Write-Output $array1[2]

$array2 = New-Object "object[]" 3
$array2[0] = "項目1"
$array2[1] = "項目2"
$array2[2] = "項目3"
Write-Output $array2[0]
Write-Output $array2[1]
Write-Output $array2[2]

$hash_array1 = @{"みかん" = "オレンジ色" ; "イチゴ" = "赤色" ; "レモン" = "黄色"}
Write-Output $hash_array1["イチゴ"]

$hash_array2 = @{}
$hash_array2.Add("みかん","オレンジ色")
$hash_array2.Add("イチゴ","赤色")
$hash_array2.Add("レモン","黄色")
Write-Output $hash_array2["レモン"]

004_operator.ps1

$a = "文字列1"
$b = "文字列2"
$c = $a + $b
Write-Output $c

$a = "文字列1"
$b = 3
$c = $a * $b
Write-Output $c

$a = 5
$b = 2
$c = $a + $b
Write-Output $c

$a = 5
$b = 2
$c = $a - $b
Write-Output $c

$a = 5
$b = 2
$c = $a * $b
Write-Output $c

$a = 5
$b = 2
$c = $a / $b
Write-Output $c

$a = 5
$b = 2
$c = $a % $b
Write-Output $c

$c = [Math]::Pow($a, $b)
Write-Output $c

$a++
Write-Output $a

$b--
Write-Output $b

005_if.ps1

$a = 100

if ($a -eq 10){
	Write-output '$a is 10'
}elseif ($a -eq 100){
	Write-output '$a is 100'
}else{
	Write-output '$a is unknown'
}

if ($a -ne 10){
	Write-output '$a is not 10'
}

if ($a -gt 10){
	Write-output '$a is greater than 10'
}

if ($a -ge 10){
	Write-output '$a is greater equal than 10'
}

if ($a -lt 200){
	Write-output '$a is less than 200'
}

if ($a -le 200){
	Write-output '$a is less equal than 200'
}

006_while.ps1

$a = 0
while ($a -lt 5){
	Write-output $a
	$a++
}

007_for.ps1

for ($a = 0; $a -lt 5; $a++){
	Write-Output $a
}

$array = @(10,20,30)
foreach ($i in $array){
  $sum += $i;
}
Write-Output $sum

$hash_array = @{"りんご" = "赤色"; "レモン" = "黄色"; "みかん" = "オレンジ色"}
foreach ($i in $hash_array.keys){
	Write-Output $hash_array[$i]
}

008_file.ps1

Write-Output "1行目" | Out-File -encoding Default 008_file.txt
Write-Output "2行目" | Out-File -encoding Default -Append 008_file.txt
Write-Output "3行目" | Out-File -encoding Default -Append 008_file.txt

$line = 0
Get-Content 008_file.txt |
ForEach-Object {
	$line++
	$out = "$line => $_"
	Write-Output $out
}

$objR = New-Object System.IO.StreamReader("008_file.txt", [Text.Encoding]::GetEncoding("Shift_JIS"))
$append_sw = $false
$objW = New-Object System.IO.StreamWriter("008_file2.txt", $append_sw,[Text.Encoding]::UTF8)
while (($rec = $objR.readLine()) -ne $null){
	$objW.writeLine($rec)
}
$objR.close()
$objW.close()

009_regex.ps1

$a = "abcde"
if ($a -match "^a.*e"){
	Write-Output "matched!"
}

if ($a -notmatch "^a.*e"){
	Write-Output "not matched!"
}else{
	Write-Output "matched!"
}

$b = $a -replace "b.*d","BCD"
Write-Output $b

$b = $a -replace '(b).*d','$1CD'
Write-Output $b

$a = "文字列1,文字列2,,文字列3"
$array = $a -split ",,*"
foreach ($i in $array){
	Write-Output $i
}

010_string.ps1

$a = "abcdefg"
Write-Output $a.Length
Write-Output $a.substring(2,3)

011_function.ps1

function tasizan($a, $b){
	Write-Output ($a + $b)
}

$out = tasizan 10 20
Write-Output "10 + 20 = $out"

Get-Content *.ps1 |
&{
	begin{
		$count = $args[0]
		$step = $args[1]
	}
	process{
		write-output "$count $_"
		$count += $step
	}
	end{
	}
} 100 10

012_daytime.ps1

$out = get-date -uformat "%Y/%m/%d %H:%M:%S(%w)"
Write-Output $out

013_arg.ps1

for ($i = 0; $i -le $args.length; $i++){
	Write-Output $args[$i]
}

101_cat.ps1

$line = 0
Get-Content *.ps1 |
ForEach-Object {
	$line++
	$out = "${line}: $_"
	Write-Output "$out"
}

102_wcl.ps1

$file = Get-Content 001*ps1
Write-Output $file.Count

(get-content 001*ps1).count

Get-Content 001*ps1 |
Measure-Object |
ForEach-Object{$_.Count}

103_cut.ps1

Get-Content 103_cut.csv |
ForEach{
	$_.split(",")[4] + "," + $_.split(",")[3]
}

Get-Content 103_cut.csv |
ConvertFrom-Csv -Delimiter "," -Header "mode","date","time","size","name"  |
Select-Object name,size |
ConvertTo-Csv -Delimiter ","

104_grep.ps1

Get-Content .\103_cut.csv |
select-string "005_.*ps1"

select-string "005_.*ps1" 103_cut.csv

select-string -NotMatch "005_.*ps1" 103_cut.csv

105_head.ps1

Get-Content 103_cut.csv |
Select-Object -First 3

106_sed.ps1

Get-Content .\103_cut.csv |
foreach{
	$_ -replace ".ps1$",".PS1"
}

107_sel.ps1

Get-Content 103_cut.csv |
ForEach{
	if ([int]$_.split(",")[3] -gt 100){
		$_.split(",")[4] + "," + $_.split(",")[3]
	}
}

Get-Content 103_cut.csv |
ConvertFrom-Csv -Delimiter "," -Header "mode","date","time","size","name"  |
Where-Object {[int]$_.size -gt 100}|
Select-Object name,size |
ConvertTo-Csv -Delimiter ","

108_sort.ps1

Get-Content 103_cut.csv |
ConvertFrom-Csv -Delimiter "," -Header "mode","date","time","size","name"  |
Sort-Object size |
ConvertTo-Csv -Delimiter ","

Get-Content 103_cut.csv |
ConvertFrom-Csv -Delimiter "," -Header "mode","date","time","size","name"  |
Sort-Object {[int]$_.size} |
ConvertTo-Csv -Delimiter ","

109_tee.ps1

Get-Content 103_cut.csv |
Tee-Object 109_tee.txt

110_tail.ps1

Get-Content 103_cut.csv |
Select-Object -Last 3

111_uniq.ps1

function uniqd (){
	begin{
		$first_sw = "on"
	}
	process{
		if ($first_sw -eq "on"){
			$rec = $_
			$first_sw = "off"
			$dup_sw = "off"
		}else{
			if ($rec -eq $_){
				$dup_sw = "on"
			}else{
				if ($dup_sw -eq "on"){
					write-output $rec
				}
				$rec = $_
				$dup_sw = "off"
			}
		}
	}
	end{
		if ($dup_sw -eq "on"){
			write-output $rec
		}
	}
}

Get-Content 111_uniq.txt |
Select-Object -Unique

Get-Content 111_uniq.txt |
Sort-Object |
uniqd

Get-Content 111_uniq.txt |
Sort-Object |
&{
	begin{
		$first_sw = "on"
		$count = 0
	}
	process{
		if ($first_sw -eq "on"){
			$rec = $_
			$count++
			$first_sw = "off"
		}else{
			if ($rec -eq $_){
				$count++
			}else{
				write-output "$count $rec"
				$count = 0

				$rec = $_
				$count++
			}
		}
	}
	end{
		write-output "$count $rec"
	}
}

112_xls2csv.ps1

$intSheetNum = 1
$strSheetName = "Sheet1"

Get-ChildItem *.xls* |
ForEach-Object {
	$InPath = resolve-path $_.Name
	$OutPath = $InPath -replace ".xls.*", ".csv"
	$objExcel = New-Object -ComObject Excel.Application
	$objExcel.DisplayAlerts = $false

	$objExcel.Workbooks.open($InPath)

#	$objSheet = $objExcel.Worksheets.Item($intSheetNum)
	$objSheet = $objExcel.Worksheets.Item($strSheetName)
	$objSheet.SaveAs($OutPath, 6)
	$objExcel.Workbooks.Close()
	$objExcel.Quit()
}