Hands-on learning record blog

ハンズオン学習記録、更新のない日は技術本を読む📚

6/25(火)Linux学習

・シェル変数と環境変数

→シェルは、一時的な変数であるシェル変数にテキスト文字列の値を格納できる。

[cloudshell-user@ip-10-132-44-98 ~]$ echo shell_var
shell_var
[cloudshell-user@ip-10-132-44-98 ~]$ shell_var=test123
[cloudshell-user@ip-10-132-44-98 ~]$ echo $shell_var
test123
[cloudshell-user@ip-10-132-44-98 ~]$ 

 

※シェル変数は、シェルを切り替えた際に設定は引き継がれない

→シェルを切り替えたときに保持しておきたい場合に設定するのが「環境変数

 

環境変数

→printenvコマンドもしくは、envコマンドのみで取得される

→shell_varを環境変数として保存したい場合は、export 変数名

 

・&&演算子

→前のコマンドが成功した場合のみに次のコマンドを実行する

 

・||演算子

→左側にコマンドが失敗した場合のみ、右側のコマンドが実行される

 

・シングルクォーテーション

→囲んだコマンドを文字列として表示

[cloudshell-user@ip-10-132-44-98 ~]$ echo 'Hello World!'
Hello World!
[cloudshell-user@ip-10-132-44-98 ~]$ 

 

・バッククォーテーション

→その間のコマンドを実行し、その結果を文字列として扱う

[cloudshell-user@ip-10-132-44-98 ~]$ echo "本日の日付:`date`"
本日の日付:Tue Jun 25 12:12:51 PM UTC 2024
[cloudshell-user@ip-10-132-44-98 ~]$ 

 

・ダブルクォーテーション

→その間の文字列をそのまま扱いながら、変数の展開やエスケープシーケンスと呼ばれる解釈を行う

[cloudshell-user@ip-10-132-44-98 ~]$ name="Reo"
[cloudshell-user@ip-10-132-44-98 ~]$ echo $name
Reo
[cloudshell-user@ip-10-132-44-98 ~]$ echo "名前は $name です"
名前は Reo です
[cloudshell-user@ip-10-132-44-98 ~]$

 

・vi

Linuxでテキストファイルを編集するツール

→iキーを入力し、インサートモード(挿入モード)に切り替える

→EscキーもしくはCtrl+cでインサート表示が消える

→:w(コロン、wキー)で現在編集している内容を保存するコマンド

 "test" [New] 4L, 41B written(4行、41バイト書き込んだ表示)

→:q(コロン、qキー)でviを終了する

以下は、cat testでviで編集し保存した内容が表示される

[cloudshell-user@ip-10-132-44-98 ~]$ cat test
aaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

[cloudshell-user@ip-10-132-44-98 ~]$

 

→:wq(コロン、w+qキー)で保存して終了するコマンド

- 主な流れ -

①vi 「テキスト名」でviモード

②テキストを編集する

③iキーでインサートモードに切り替え

➃保存、終了コマンドを実行

 

※:wコマンド、スペース、存在しないファイル名を入力すると別名で保存できる

 

→:q!(ビックリマークで強制的に実行)

 

→取り消ししたい場合は、Ctrl+Cを押してからUキーで取り消しできる

→戻しすぎた場合は、Ctrl+Rでredoの動きとなる

→yyで行全体のコピー、pで1行下にペースト、ddで行全体のカット

→Shift+Vでビジュアルモードで行全体を選択できる

→:vert terminaで画面を分割できる

 

・nano

拡張機能

→Ctrl+Oで保存、Ctrl+Xで終了

 

Linuxではソフトウェアのことをパッケージと呼ぶ

→パッケージ管理コマンドには、yumやdnfがある

→これらのコマンドを実行すると、今何のソフトウェアがインストールされているか確認ができ、新しくソフトウェアをダウンロードすることも可能。

 

リポジトリ

→ソフトウェア(パッケージ)保管場所のこと

スーパーユーザへ切り替えし、dnfコマンドでインストールしたいパッケージを指定

[cloudshell-user@ip-10-132-44-98 ~]$ sudo su -
[root@ip-10-132-44-98 ~]# dnf install httpd
Last metadata expiration check: 2:28:22 ago on Tue 25 Jun 2024 11:09:07 AM UTC.
Dependencies resolved.
=======================================================================================================================================
 Package                             Architecture           Version                                  Repository                   Size
=======================================================================================================================================
Installing:
 httpd                               x86_64                 2.4.59-2.amzn2023                        amazonlinux                  47 k
Installing dependencies:
 apr                                 x86_64                 1.7.2-2.amzn2023.0.2                     amazonlinux                 129 k
 apr-util                            x86_64                 1.6.3-1.amzn2023.0.1                     amazonlinux                  98 k
 generic-logos-httpd                 noarch                 18.0.0-12.amzn2023.0.3                   amazonlinux                  19 k
 httpd-core                          x86_64                 2.4.59-2.amzn2023                        amazonlinux                 1.4 M
 httpd-filesystem                    noarch                 2.4.59-2.amzn2023                        amazonlinux                  14 k
 httpd-tools                         x86_64                 2.4.59-2.amzn2023                        amazonlinux                  81 k
 mailcap                             noarch                 2.1.49-3.amzn2023.0.3                    amazonlinux                  33 k
Installing weak dependencies:
 apr-util-openssl                    x86_64                 1.6.3-1.amzn2023.0.1                     amazonlinux                  17 k
 mod_http2                           x86_64                 2.0.27-1.amzn2023.0.2                    amazonlinux                 166 k
 mod_lua                             x86_64                 2.4.59-2.amzn2023                        amazonlinux                  61 k

Transaction Summary
=======================================================================================================================================
Install  11 Packages

Total download size: 2.0 M
Installed size: 6.2 M
Is this ok [y/N]:

 

→あるパッケージをインストールしたい場合に動くために必要な別のパッケージも必要となることがある。

→そのような別パッケージを「依存パッケージ」と呼んでいる

 

●通常の依存 Installing dependencies(依存関係)

→これらがないと動かなくなるから、絶対必要

●弱い依存 Installing weak dependencies

→なくてもクリティカルな致命傷にはならないけど、入れておいたほうがいい

 

→dnf updateでパッケージのバージョンを更新できる

→dnf removeでパッケージの削除

→dnf searchでパッケージの検索

→dnf infoでパッケージの詳細表示

 

・topコマンド

→CPU使用率、実行中のプロセスに対するCPU使用率など詳細な情報を表示できる

→qキーで抜けれる

 

・htopコマンド

→topコマンドが進化したコマンド

 

・常駐起動するソフトウェアをデーモンと呼ぶ

デーモン=守護神のように常に動いて常に守る役割を担っている

 

・systemctl list-units --type=service

→すべてのアクティブなサービス(ソフトウェア)をリストアップするコマンド

→すべてのサービスとその状態のリストを表示する

 

・systemctl status

→プロセスの詳細情報を表示できる

 

・systemctl start 起動

・systemctl stop 停止

・systemctl restart 再起動

・systemctl enable 自動起動