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

Opening Git Bash

$ echo Hello, shello!
Hello, shello!

this is wired, isn’t it?

$ echo Hello, shello!!
echo Hello, shelloecho Hello, shello!
Hello, shelloecho Hello, shello!

improve with single quote

$ echo 'Hello, shello!!'
Hello, shello!!

ls = list
$ ls
$ ls Downloads

cd = Change Directory
$ cd Downloads
$ cd .. ; ls

pwd = Print Working Directory
“ls .” is just same as “ls”
$ ls .

all of the files matched pdf.
$ ls -l Documents/*.pdf

$ mkdir Documents/Books
$ mv 'Documents/1911 Webster Dictionary.epub' Documents/Books/
$ mv Documents/*.epub Documents/Books

Unix shell

> echo "Hello, $LOGNAME"
Hello, Karl
> git clone https://github.com/*

Needs to remember the words like below
less, ls, pwd, mkdir, cat, curl, echo, git

console.log("hello!");
hello!

bash

shell script

linux shell script
シェルスクリプトを操作していきます。

[vagrant@localhost shell]$ echo $SHELL
/bin/bash
[vagrant@localhost shell]$ vi hello.sh

vimを使って、hello.shを書き込みます。正常終了でexit 0とします。コメントは#を使います。

#!/bin/bash

echo "hello world"
exit 0

実行権限を与えます。

[vagrant@localhost shell]$ chmod +x hello.sh
[vagrant@localhost shell]$ ./hello.sh
hello world

変数

#!/bin/bash

s="hello"
echo $s$s
exit 0

数値演算はバッククォート`(shift + @)expr `で処理します。

#!/bin/bash

x=10
echo `expr $x + 2`
exit 0

掛け算は、\でエスケープが必要です。()も\( $x + 5 \)と\のエスケープが必要です。
echo `expr $x \* 2`

配列は添え字を使います。全ての要素は@を使います。

a=(2 4 6)
echo ${a[2]}

echo ${a[@]}
echo ${#a[@]}
exit 0

代入、要素の追加

a[2] = 10
a+=(20, 30)

-eqで正しいか否かの条件分岐を行っています。0は正常、1は不正となります。
-eq, -ne, -gt, -ge, -lt, -le、=, !=, -nt, -ot, -e, -dなどのコマンドがあります。

#!/bin/bash

test 1 -eq 2; echo $?

test -e new.sh; echo $?
論理演算子には、-a(and),-o(or),!などあります。

ifの条件分岐

#!/bin/bash

x=70
if test $x -gt 60
then
 echo "ok!"
fi
x=70
if [ $x -gt 60 ]; then
 echo "ok!"
else
 echo "soso.."
fi

case条件分岐

signal="red"
case $signal in
 "red")
  echo "stop!"
  ;;
  "yellow")
  echo "caution"
  ;;
  "green")
  echo "go!"
  ;;
  *)
  echo "...."
  ;;
esac

while文

i=0
while [ $i -lt 10 ]
do
 i=`expr $i + 1`
 echo $i
done

for文: `seq 1 100`

a=(1 2 3 4 5)
for i in ${a[@]}
do
 echo $i
done

コマンド引数を使うと柔軟にプログラムを書けるようになります。
ユーザからの入力を受け付ける。

while :
do
 read key
 echo "you pressed $key"
 if [ $key = "end" ]; then
  break
 fi
done

ファイルの読み込み

i=1
while read line
do
 echo "$i: $line"
 i=`expr $i + 1`
done <$1

関数

function hello(){
 echo "hello"
}

hello