Linux リテラシ - 第2回 演習問題 解答
This content is not available in your language yet.
:問題(1):
viでex.shを作成し、
#!/bin/sh## shell#dateecho "This is a sample of shell script."を書き込み保存せよ。
[user@localhost ~]$ vi ex.shインサートモードに変更し、解答を打ち込むコマンドラインモードに戻り、:wqで内容を保存する:問題(2):コンソール画面を開き、whichを用いてechoコマンドが実際に配置されている場所をたしかめよ。
[user@localhost ~]$ which echo/bin/echo:問題(3):絶対パスでechoコマンドを実行し、PATHの内容を表示させよ。(PATHの表示には$PATHを用いる)
[user@localhost ~]$ /bin/echo $PATH:問題(4):サンプルファイルをダウンロード、解凍し、サンプルファイルex01をlessを用いて閲覧し、文字列putを検索せよ。(/文字列検索をする、解凍には前回使用したtarコマンドを使う)
[user@localhost ~]$ wget http://rat.cis.k.hosei.ac.jp/data/edsh.tar.gz[user@localhost ~]$ tar -zxf edsh.tar.gz[user@localhost ~]$ less samples/ex01lessビューアを開き/putを用いて検索します。:問題(5):サンプルファイルex01をviを用いて開き、文字列putを検索せよ。
[user@localhost ~]$ vi samples/ex01viエディタを開き/putを用いて検索します。:問題(6):サンプルファイルex01の中のputを用いている行をgrepを用いて表示させよ。
[user@localhost ~]$ grep put samples/ex01 puts("vsed : can not open file."); puts("date(yy/mm/dd) : "); fputs(buf, fp); fputc('\n', fp); puts("text : "); fputs(buf, fp); fputc('\n', fp); puts("usage : vsed <filename>");:問題(7):サンプルファイルex01の中のputを用いている行を新規ファイルtestに書き込め。(出力のリダイレクトを用いる)
[user@localhost ~]$ grep put samples/ex01 > test:問題(8):サンプルファイルex01からex09までのファイルの中でincludeを用いている行を表示させよ。(グロブを用いる)
[user@localhost ~]$ grep include samples/ex0[1-9]samples/ex01:#include <stdio.h>samples/ex02:#include <stdio.h>samples/ex02:# include <string.h>samples/ex03:#include <stdio.h>samples/ex03:#include <string.h>samples/ex03:#include <time.h>samples/ex03:#include <unistd.h>samples/ex03:#include <pwd.h>samples/ex03:#include <sys/types.h>samples/ex04:#include <stdio.h>samples/ex04:#include <string.h>samples/ex04:#include <time.h>samples/ex04:#include <unistd.h>samples/ex04:#include <pwd.h>samples/ex04:#include <sys/types.h>samples/ex05:#include <stdio.h>samples/ex05:#include <sys/utsname.h>samples/ex06:#include <stdio.h>samples/ex06:#include <string.h>samples/ex06:#include <time.h>samples/ex06:#include <unistd.h>samples/ex06:#include <pwd.h>samples/ex06:#include <sys/types.h>samples/ex07:#include <stdio.h>samples/ex07:#include <math.h>samples/ex08:#include <stdio.h>samples/ex08:#include <string.h>samples/ex08:#include <time.h>samples/ex08:#include <unistd.h>samples/ex08:#include <pwd.h>samples/ex08:#include <sys/types.h>samples/ex09:#include <stdio.h>samples/ex09:#include <sys/utsname.h>
上記のグロブを使用するコマンドは[user@localhost ~]$ grep include samples/ex0?[user@localhost ~]$ grep include samples/ex0*のようにしてもOKです。:問題(9):サンプルファイルex01からex09までのファイルの中でincludeを用いている行を抜き出しlessエディタで表示させよ。(1行のコマンドでパイプ、グロブを用いる)
[user@localhost ~]$ grep include samples/ex0[1-9] | less
上記のグロブを使用するコマンドは[user@localhost ~]$ grep include samples/ex0? | less[user@localhost ~]$ grep include samples/ex0* | lessのようにしてもOKです。問題(8)の結果をlessエディタで見れれば成功です。:問題(10):サンプルファイルex01からex09までのファイルの中でincludeを用いている行の重複を取り除き、アルファベット順に並び替えた結果をファイルsortedに出力せよ。(一行のコマンドで、コマンドsortを用いる)
[user@localhost ~]$ grep -h include samples/ex0[1-9] | sort -u > sorted[user@localhost ~]$ cat sorted#include <math.h>#include <pwd.h>#include <stdio.h># include <string.h>#include <string.h>#include <sys/types.h>#include <sys/utsname.h>#include <time.h>#include <unistd.h>ファイル内容が以上のようになっていれば成功です(sortのバージョンによって、順序が変わる可能性があります)
上記のグロブを使用するコマンドは[user@localhost ~]$ grep -h include samples/ex0? | sort -u > sorted[user@localhost ~]$ grep -h include samples/ex0* | sort -u > sortedのようにしてもOKです。:問題(11):ex.shのパーミッションを自分が書き込みできないように変更し、viで書き込みが出来ない事をたしかめよ。(chmodを用いる)
[user@localhost ~]$ chmod u-w ex.sh[user@localhost ~]$ vi ex.shviエディタを開き、コマンドモードで:wを行い、 (add ! to override) の警告が出ればOKです。上記のchmodの部分は[user@localhost ~]$ chmod 544 ex.sh等でも可能です。:問題(12):ex.shのパーミッションを自分が読み込みできないように変更し、ビューアで読み込めないことを確かめよ。
[user@localhost ~]$ chmod u-r ex.sh[user@localhost ~]$ less ex.shex.sh: Permission deniedと表示されればOKです。上記のchmodの部分は[user@localhost ~]$ chmod 344 ex.sh等でも可能です。:問題(13):
ex.shのパーミッションを自分が実行できるように変更し、ex.shの配置してあるディレクトリに移動し
./ex.shが実行できる事をたしかめよ。
[user@localhost ~]$ chmod 544 ex.sh[user@localhost ~]$ ./ex.shThu Jun 2 10:53:20 JST 2005This is a sample of shell script.:問題(14):ls -aに対してlaという別名をつけよ。そして、その動作を確かめよ。(aliasコマンドを用いる)
[user@localhost ~]$ alias la='ls -a'[user@localhost ~]$ la . .bash_logout .canna mstscax.dll .ssh.. .bash_profile .emacs mstsc.exe .viminfo.bash_history .bashrc以上のようにlaコマンドを実行した結果がls -aコマンドを実行したのと同じ結果が出れば完了です。:問題(15):別名laを削除せよ。(unaliasコマンドを用いる)
[user@localhost ~]$ unalias la:問題(16):aliasコマンドを打ち込み現在のaliasの値を確認せよ。
[user@localhost ~]$ aliasalias l.='ls -d .* --color=tty'alias ll='ls -l --color=tty'alias ls='ls --color=tty'alias vi='vim'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde':問題(17):lsにunaliasをかけ、かける前と何が違うのか考察せよ。
[user@localhost ~]$ unalias ls--color=ttyが消去されることによって、lsコマンドの実行結果に色が付かなくなります。