shellでselect

selectを使おう

#!/bin/bash

select color in red blue yellow green; do
case "$color" in
	red)
		echo "stop"
		;;
	blue)
		echo "go"
		;;
	yello)
		echo "caution"
		;;
	*)
		echo "wrong signal"
		break
esac
done

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
1) red
2) blue
3) yellow
4) green
#? 2
go
#? 4
wrong signal

#!/bin/bash

hello(){
  echo "hello ... $1"
}

hello yoshida

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
hello … yoshida

UNIX系(Linux,Mac)はシェルスクリプト、Windowsはバッチ処理
シェルスクリプトに処理を書いていき、実行する

数値比較

#!/bin/bash

read -p "Number? " n
if ((n > 10)); then
	echo "bigger than 10"
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
Number? 12
bigger than 10

#!/bin/bash

for i in {1..5};
do
	echo $i
done

#!/bin/bash

for ((i=1; i<10; i++)); do echo $i done [/code] [code] #!/bin/bash for item in $(date); do echo $item done [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello 2018年 9月 30日 日曜日 12:11:36 JST [code] #!/bin/bash i=1 while read line; do echo $i "$line" ((i++)) done < colors.txt [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello 1 red 2 blue [code] #!/bin/bash read -p "Signal color? " color case "$color" in red) echo "stop" ;; blue) echo "go" ;; yello) echo "caution" ;; *) echo "wrong signal" esac [/code] [vagrant@localhost shell]$ sed -i 's/\r//' hello [vagrant@localhost shell]$ ./hello Signal color? red stop

特殊変数

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello yoshida
hello yoshida

#!/bin/bash

echo "hello $1"

$1, $2などを使う

#!/bin/bash

echo "hello $1"

echo $0
echo $#
echo $@

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello a aa aaa
hello a
./hello
3
a aa aaa

ユーザーからの入力を受け取る

#!/bin/bash

read -p "名前: " name
echo "hello ${name}"

[vagrant@localhost shell]$ ./hello
名前: 田中
hello 田中

なんじゃこりゃーー

#!/bin/bash

read -p "Pick 3 colors: " c1 c2 c3
echo $c1
echo $c2
echo $c3

[vagrant@localhost shell]$ ./hello
Pick 3 colors: red geen blue
red
geen
blue

配列

#!/bin/bash

colors=(red blue pink)
colors[1]=silver
colors+=(green orange)
echo ${colors[@]}

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
red silver pink green orange

数値計算

#!/bin/bash

echo $((5 + 2))

n=10
((n=n + 2))
echo $n

if文

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ ./hello
Name? yoshida
welcome

#!/bin/bash

read -p "Name? " name
if [ "$name" = "yoshida" ] 
then
	echo "welcome"
elif [ "$name" = "kobayashi" ] 
then
	echo "welcome, too"
else
	echo "you are not allowed"
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
Name? kobayashi
welcome, too

[vagrant@localhost shell]$ ./hello
Name?
empty …

ファイルを比較

#!/bin/bash

if [[ -f $0 ]]; then
	echo "file exists ..."
fi

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
file exists …

shellを使っていこう

[vagrant@localhost shell]$ which bash
/bin/bash

[vagrant@localhost shell]$ cat -A hello
#!/bin/bash^M$
^M$
echo hello

^Mがあるのがよろしくない

tr(ティーアール)はUNIXおよびUNIX系システムのコマンドである。名称は translate または transliterate の略。

tr は標準入力から読み込んで標準出力に出力する。パラメータとして2つの文字集合を指定し、一方の文字集合に含まれる文字が出現する度に、もう一方の文字集合の同じ位置にある文字に置換して出力する。

sed(セド)は、入力ストリーム(ファイルまたはパイプラインからの入力)に対してテキスト変換などのデータ処理をおこなうために使用されるプログラム

文字列の置き換えは「s」コマンドを使って、「s/置換前/置換後/」と指定します。例えば、文字列「GNU」を「gnu」に置き換えるならば、「s/GNU/gnu/」と指定します。

[vagrant@localhost shell]$ sed -e s/^M// hello
#!/bin/bash

echo hello

改行コードを変換する

[vagrant@localhost shell]$ sed -i 's/\r//' hello
[vagrant@localhost shell]$ cat -e hello
#!/bin/bash$
$
echo hello[vagrant@localhost shell]$ ./hello
hello

変数を使用する

#!/bin/bash

name="yoshida"

echo "hello world $name"
echo "foo ${name}san"; echo "bar"

[vagrant@localhost shell]$ sed -i ‘s/\r//’ hello
[vagrant@localhost shell]$ ./hello
hello world yoshida
foo yoshidasan
bar

シェルスクリプト

#!/bin/sh
echo "Hello World!"

./hello.sh で動かします。
[vagrant@localhost app]$ ./hello.sh
-bash: ./hello.sh: 許可がありません

permission denied.

[vagrant@localhost app]$ cat -e hello.sh
#!/bin/sh^M$
echo “Hello World!”

改行コードがおかしいみたい。
[vagrant@localhost app]$ sed -i ‘s/\r//’ hello.sh
[vagrant@localhost app]$ ./hello.sh
Hello World!

Power-shellでgrepのようなことをしたい

powershellを起動し、Select-String “search name”で検索する。

file

powershell

> select-string "server" koneksi.php

koneksi.php:3:  $server         = "localhost"; //sesuaikan dengan nama server
koneksi.php:8:  $connect = mysql_connect($server, $user, $password) or die ("Koneksi gagal!");
koneksi.php:12: // $con = mysqli_connect($server, $user, $password, $database);

select-string はslsでも代替可能。

> sls "user" koneksi.php

koneksi.php:4:  $user           = "root"; //sesuaikan username
koneksi.php:8:  $connect = mysql_connect($server, $user, $password) or die ("Koneksi gagal!");
koneksi.php:12: // $con = mysqli_connect($server, $user, $password, $database);

unixのcatとのようなコマンドはmore

n> more koneksi.php
        /* ===== www.dedykuncoro.com ===== */
        $server         = "localhost"; //sesuaikan dengan nama server
        $user           = "root"; //sesuaikan username
        $password       = "enter"; //sesuaikan password
        $database       = "kuncoro_login"; //sesuaikan target databese

        $connect = mysql_connect($server, $user, $password) or die ("Koneksi gagal!");
        mysql_select_db($database) or die ("Database belum siap!");

        /* ====== UNTUK MENGGUNAKAN MYSQLI DI UNREMARK YANG INI, YANG MYSQL_CONNECT DI REMARK ======= */
        // $con = mysqli_connect($server, $user, $password, $database);
        // if (mysqli_connect_errno()) {
        //      echo "Gagal terhubung MySQL: " . mysqli_connect_error();
        // }

複数ファイルを検索する場合は、ワイルドカードを使う。

 sls "user" *.php

koneksi.php:4:  $user           = "root"; //sesuaikan username
koneksi.php:8:  $connect = mysql_connect($server, $user, $password) or die ("Koneksi gagal!");
koneksi.php:12: // $con = mysqli_connect($server, $user, $password, $database);
login.php:7:    $username = $_POST["username"];
login.php:10:   if ((empty($username)) || (empty($password))) {
login.php:17:   $query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
login.php:24:           $response->message = "Selamat datang ".$row['username'];
login.php:26:           $response->username = $row['username'];
login.php:32:           $response->message = "Username atau password salah";
login.php:44:   // $username = $_POST["username"];
login.php:47:   // if ((empty($username)) || (empty($password))) {
login.php:54:   // $query = mysqli_query($con, "SELECT * FROM users WHERE username='$username' AND password='$password'")
;
login.php:61:   //      $response->message = "Selamat datang ".$row['username'];
login.php:63:   //      $response->username = $row['username'];
login.php:69:   //      $response->message = "Username atau password salah";
register.php:7: $username = $_POST["username"];
register.php:11:        if ((empty($username))) {
register.php:14:                $response->message = "Kolom username tidak boleh kosong";
register.php:27:                if (!empty($username) && $password == $confirm_password){
register.php:28:                        $num_rows = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='".$username."'"));
register.php:31:                                $query = mysql_query("INSERT INTO users (id, username, password) VALUES(0,'".$username."','".$passw
ord."')");
register.php:42:                                        $response->message = "Username sudah ada";
register.php:48:                                $response->message = "Username sudah ada";
register.php:62:        // $username = $_POST["username"];
register.php:66:        // if ((empty($username))) {
register.php:69:        //      $response->message = "Kolom username tidak boleh kosong";
register.php:82:                // if (!empty($username) && $password == $confirm_password){
register.php:83:                //      $num_rows = mysqli_num_rows(mysqli_query($con, "SELECT * FROM users WHERE username='".$username."
'"));
register.php:86:                //              $query = mysqli_query($con, "INSERT INTO users (id, username, password) VALUES(0,'".$username."'
,'".$password."')");
register.php:97:                //                      $response->message = "Username sudah ada";
register.php:103:               //              $response->message = "Username sudah ada";

ほお~

grep

$ curl -L https://tinyurl.com/zeyq9vc | grep fish
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0
1 916k 1 16384 0 0 11778 0 0:01:19 0:00:01 0:01:18 11778bluefish
bluefish’s
bluefishes
catfish
catfish’s
catfishes
codfish
codfish’s
codfishes
crawfish
crawfish’s
crawfishes
crayfish
crayfish’s
crayfishes
cuttlefish
cuttlefish’s
cuttlefishes
dogfish
dogfish’s
dogfishes
dwarfish
elfish
fish
fish’s
fishbowl
fishbowl’s
fishbowls
fished
fisher
fisher’s
fisheries
fisherman
fisherman’s
fishermen
fishers
fishery
fishery’s
fishes
fishhook
fishhook’s
fishhooks
fishier
fishiest
fishing
fishing’s
fishnet
fishnet’s
fishnets
fishtail
fishtailed
fishtailing
fishtails
fishwife
fishwife’s
fishwives
fishy
flatfish
flatfish’s
flatfishes
goldfish
goldfish’s
goldfishes
jellyfish
jellyfish’s
jellyfishes
kingfisher
kingfisher’s
kingfishers
62 916k 62 575k 0 0 246k 0 0:00:03 0:00:02 0:00:01 596koafish
raffish
sailfish
sailfish’s
sailfishes
selfish
selfishly
selfishness
selfishness’s
shellfish
shellfish’s
shellfishes
silverfish
silverfish’s
silverfishes
standoffish
starfish
starfish’s
starfishes
sunfish
sunfish’s
sunfishes
swordfish
swordfish’s
swordfishes
unselfish
unselfishly
unselfishness
unselfishness’s
weakfish
weakfish’s
weakfishes
whitefish
whitefish’s
whitefishes
wolfish
100 916k 100 916k 0 0 341k 0 0:00:02 0:00:02 –:–:– 694k

$ numers='one two three'
$ echo $numers
one two three

$ echo $LINES x $COLUMNS
24 x 80
$ echo $PATH
$ ls bin
http://bashrcgenerator.com

$ alias ll=’ls -la’
$ ll

The Bash Academy
Bash Beginners Guide
Bash Programming HOWTO
Regexr — Learn Regular Expressions

Cat

cat -> catenate, Concatenate
$ cat dictionary.txt

rm -> remove
$ rm SillyFile.txt
$ rm -i ValuableFile.txt

$ rm -i google.html
rm: remove regular file 'google.html'? y

$ rmdir hogehoge

$ grep shell dictionary.txt
bombshell
bombshell’s
bombshells
bushelled
bushelling
cockleshell
cockleshell’s
cockleshells
eggshell
eggshell’s
eggshells
nutshell
nutshell’s
nutshells
seashell
seashell’s
seashells
shell
shell’s
shellac
shellac’s
shellacked
shellacking
shellacs
shelled
sheller
shellfish
shellfish’s
shellfishes
shelling
shells
tortoiseshell
tortoiseshell’s
tortoiseshells

curl

curl -> C URL -> see URL

$ curl 'http://google.com'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   259  100   259    0     0   1106      0 --:--:-- --:--:-- --:--:--  2072
302 Moved

302 Moved

The document has moved here.
$ curl -o google.html -L 'http://google.com'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   261  100   261    0     0   1197      0 --:--:-- --:--:-- --:--:--  1864
100 10726    0 10726    0     0  17612      0 --:--:-- --:--:-- --:--:-- 85808
$ curl -L -o dictionary.txt 'https://tinyurl.com/zeyq9vc'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100  916k  100  916k    0     0   217k      0  0:00:04  0:00:04 --:--:--  598k