A shell is a program that interprets and executes commands. Work on linux system is done on the termimnal.
A program(shell) that interprets and executes commands to be entered for each terminal when loggin in to the linux system. It starts to work. There are several kind of shells, and users can select a shell to execute according to their preference.
Category: shell
/dev/null 2>&1
#/bin/bash echo "this is standard output"
[vagrant@localhost app]$ ./echo.sh
-bash: ./echo.sh: /bin/sh^M: bad interpreter: そのようなファイルやディレクトリはありません
[vagrant@localhost app]$ sed -i ‘s/\r//’ echo.sh
[vagrant@localhost app]$ ./echo.sh
this is standard output
It took an hour to come here.
If it is executed normally, it will be displayed on the standard output, but if you redirect it, it will be able to output to another location.
When redirecting to standard error output
[vagrant@localhost app]$ ./echo.sh >&2
this is standard output
When redirecting to standard output
[vagrant@localhost app]$ ./echo.sh >&1
this is standard output
[vagrant@localhost app]$ ./echo.sh > /dev/null
/dev/null is a unix special file, it is an empty file.
Merging standard error output to standard output
2>&1
somehow I got it.
バッチ処理(英語ではBatch proccessing)とは
今更感がありますが。。
バッチ処理:英語ではBatch proccessing
– 一つの流れのプログラムを順次実行すること。
– 予め定められた処理を一度に行う。
PC起動時の処理などもバッチ処理という。
というこは、クローンタブで時間を定めて処理することだけをバッチ処理とは言わない、もっと広義な意味を含めているようですな。
ざっくりというと、まとめて処理ですが、場面によって微妙に違いますね。
ps auxで物理メモリの使用量を把握しよう
[vagrant@host ~]$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 19232 1504 ? Ss 15:26 0:00 /sbin/init
RSSがkbite
やべ、ブログ始めて2年半、これは素人ではなくなってきたな。
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";
ほお~