仕事支援csh

ファイルを表示する。

ドットファイルも含むすべてのファイルを表示する。

testuser$ ls
test.txt
testuser$ ls -a
.    ..    .test    test.txt

ファイルの詳細を表示する。

testuser$ ls -l
-rw-r--r--   1 testuser  testgroup  20  2月 27 22:44 test.txt

ファイルの詳細を古い順に表示する。

testuser$ ls -ltr
-rw-r--r--   1 testuser  testgroup  30  1月 25 12:24 test3.txt
-rw-r--r--   1 testuser  testgroup  20  2月 27 22:44 test.txt
-rw-r--r--   1 testuser  testgroup  25  3月 10 23:45 test2.txt

ディレクトリを階層的にファイルの詳細を表示する。

testuser$ ls -lR testdir
total 2
-rw-r--r--  1 testuser  testgroup  20  2月 27 22:44 test.txt
-rw-r--r--  1 testuser  testgroup  14  3月 15 21:30 test2.txt
drwxr-xr-x  1 testuser  testgroup   0  3月 15 21:37 testdir2

testdir/testdir2:
total 1
-rw-r--r--  1 testuser  testgroup  33  3月 15 21:37 test3.txt


ファイルに出力する。

標準出力を上書きでリダイレクトする。

testuser$ cat test.txt
bbbb
testuser$ echo aaaa > test.txt
testuser$ cat test.txt
aaaa

標準出力を追加書きでリダイレクトする。

testuser$ cat test.txt
bbbb
testuser$ echo aaaa >> test.txt
testuser$ cat test.txt
bbbb
aaaa

画面、ファイルの両方に出力する。

testuser$ echo cccc | tee test.txt
cccc
testuser$ cat test.txt
cccc

ファイルの上書きを禁止する。

testuser$ ls
test.txt
testuser$ set noclobber
testuser$ echo aaaa > test.txt
test.txt: File exists.


コマンドを訂正して実行する。

batをcatに訂正して実行する。

testuser$ bat test.txt
bat: Command not found.
testuser$ ^bat^cat
cat test.txt
aaaa


コマンドを再実行する。

直前のコマンドを再実行する。

testuser$ cat test.txt
aaaa
testuser$ !!
cat test.txt
aaaa

ヒストリ番号を指定して再実行する。

testuser$ history

77  22:26   cd testdir
78  22:26   cat test.txt
79  22:26   history
testuser$ !78
cat test.txt
aaaa

コマンドの一部を指定して再実行する。

testuser$ history

77  22:26   cd testdir
78  22:26   cat test.txt
79  22:26   history
testuser$ !ca
cat test.txt
aaaa


キーボードに入力された文字列を表示

キーボードで入力した「yamada」を表示する。

testuser$ cat test.csh
#!/bin/csh
echo -n "Enter your name > "
set name = $<
echo "Your name is " $name
testuser$ csh test.csh
Enter your name > yamada
Your name is yamada


ファイルの存在をチェックする。

test.txtというファイルが存在する場合は「exist」と表示するシェル

testuser$ cat test.csh
#!/bin/csh
if ( -e test.txt ) then
    echo "exist"
endif
testuser$ csh test.csh
exist


ファイルのバックアップを作成する。

「ファイル名_日付」というファイル名でバックアップを作成する。

testuser$ ls
test.txt
testuser$ cp -p test.txt #$_20110310
cp -p test.txt test.txt_20110310
testuser$ ls
test.txt    test.txt_20110310


サブディレクトリも含めてディレクトリを一度に作成する。

「aaa」「bbb」「ccc」ディレクトリを階層的に作成する。

testuser$ ls
test.txt
testuser$ mkdir -p aaa/bbb/ccc
testuser$ ls -R aaa
bbb

aaa/bbb
ccc

aaa/bbb/ccc



【これがオススメ】
以下のamazonリンクから購入すると齋藤緒のページ作りパワーが劇的にアップします。



名前に規則性があるディレクトリを一気に作成する。

「dir2009」「dir2010」「dir2011」ディレクトリを作成する。

testuser$ ls
test.txt
testuser$ mkdir dir20{09,10,11}
testuser$ ls
test.txt    dir2009/    dir2010/    dir2011/


名前がハイフンから始まるファイルを削除する。

「-ppp.txt」という名前のファイルを削除する。
※ rmコマンドだけでは実行時にオプション付きと判断されてエラーとなる。

testuser$ ls
-ppp.txt    test.txt
testuser$ rm -ppp.txt
rm: illegal option 'p'
usage: rm [-dfiPRr] file ...
testuser$ rm -- -ppp.txt
testuser$ ls
test.txt


補完される候補を表示する。

「ls test」の後にCTRL+Dを入力し、「test」に続くファイル名、ディレクトリ名の候補を表示する。

testuser$ ls test
test1.txt    test2.txt    testdir/
testuser$ ls test


ジョブをバックグラウンド実行する。

back.cshをバックグラウンドで実行する。

testuser$ ls
back.csh
testuser$ cat back.csh
#!/bin/csh
set i = 0
while (1)
    sleep 2
    echo $i > test.log
    @ i = $i + 1
end
testuser$ csh back.csh &
[1] 475
testuser$ ls
back.csh test.log


バックグラウンド実行されているジョブの表示と一時停止。

バックグラウンド実行したback.cshのジョブ番号を調べ、一時停止する。

testuser$ jobs
[1]  + Running             csh back.csh
testuser$ stop %1
[1]  + Suspended (signal)           csh back.csh


一時停止したジョブのバックグラウンド実行再開。

一時停止したback.cshのジョブを再開する。

testuser$ jobs
[1]  + Suspended (signal)           csh back.csh
testuser$ bg %1
[1]    csh back.csh &
testuser$ jobs
[1]  + Running             csh back.csh


バックグラウンド実行されているジョブの表示と終了。

バックグラウンド実行したback.cshのジョブ番号を調べ、終了する。

testuser$ jobs
[1]  + Running             csh back.csh
testuser$ kill %1
[1]    Terminated           csh back.csh


ログアウトしてもバックグラウンドで実行。

back.cshのジョブをログアウト後もバックグラウンドで実行させる。

testuser$ nohup csh back.csh &
[1] 585


空のファイルを削除する。

カレントディレクトリ配下で、ファイルサイズが0のファイルを検索して削除する。

testuser$ cat test.csh
#/bin/csh
foreach file ( `find .` )
    if ( -f $file && -z $file ) then
        rm -i $file
    endif
end
testuser$ ls -lR
total 2
-rw-r--r--  1 testuser  testgroup  93  3月 20 16:05 test.csh
-rw-r--r--  1 testuser  testgroup   6  3月 20 16:13 test.txt
drwxr-xr-x  1 testuser  testgroup   0  3月 20 16:13 testdir

./testdir:
total 0
-rw-r--r--  1 testuser  testgroup  0  3月 20 16:13 kara.txt
testuser$ csh test.csh
remove ./testdir/kara.txt ? y
testuser$ ls -lR
total 2
-rw-r--r--  1 testuser  testgroup  93  3月 20 16:05 test.csh
-rw-r--r--  1 testuser  testgroup   6  3月 20 16:13 test.txt
drwxr-xr-x  1 testuser  testgroup   0  3月 20 16:13 testdir

./testdir:


ファイル名で検索して削除する。

ファイル名「test.txt」を検索して削除する。

testuser$ ls -lR
total 0
drwxr-xr-x  1 testuser  testgroup  0  3月 20 16:24 testdir

./testdir:
total 1
-rw-r--r--  1 testuser  testgroup  14  3月 20 16:24 test.txt
testuser$ find . -name "test.txt" -exec rm -i {} \;
remove ./testdir/test.txt ? y
testuser$ ls -lR
total 0
drwxr-xr-x  1 testuser  testgroup  0  3月 20 16:24 testdir

./testdir:


すべてのファイルを削除する。(ディレクトリは削除しない)

カレントディレクトリ配下のファイルをすべて削除する。(サブディレクトリのファイルも含む)

testuser$ ls -lR
total 1
-rw-r--r--  1 testuser  testgroup  7  3月 20 16:32 test.txt
drwxr-xr-x  1 testuser  testgroup  0  3月 20 16:35 testdir

./testdir:
total 1
-rw-r--r--  1 testuser  testgroup  9  3月 20 16:31 test2.txt
testuser$ %find . -type f -exec rm -i {} \;
remove ./test.txt ? y
remove ./testdir/test2.txt ? y
testuser$ ls -lR
total 0
drwxr-xr-x  1 testuser  testgroup  0  3月 20 16:35 testdir

./testdir:


特定のパスを行き来する。

「/home/testuser/dir1」と「/home/testuser/dir2」を素早く移動する。

testuser$ pwd
/home/testuser/dir1
testuser$ pushd /home/testuser/dir2
~/testuser/dir2 ~/testuser/dir1
testuser$ pwd
/home/testuser/dir2
testuser$ pushd
~/testuser/dir1 ~/testuser/dir2
testuser$ pwd
/home/testuser/dir1
testuser$ pushd
~/testuser/dir2 ~/testuser/dir1
testuser$ pwd
/home/testuser/dir2


指定した文字幅で表示する。

引数で与えられた数字を8桁の文字幅でゼロ埋めし、表示する。

testuser$ cat test.csh
#!/bin/csh
set number = `printf "%08d" $1`
echo $number
testuser$ csh test.csh 12
00000012


オプション解析を行う。

他のシェルにあるgetoptsのようなシェルコマンドがCシェルには無いので、switch-case文でゴリ実装する。

testuser$ cat lsTest.csh
#!/bin/csh
while ($#argv != 0)
    switch ($1)
        case -l:
            ls -l
            breaksw
        case -a:
            ls -a
            breaksw
    endsw
    shift
end
testuser$ cat lsTest.csh -a
.    ..    .env    test.txt


システム日時を表示する。

「YYYY/MM/DD hh:mm:ss」形式でシステム年月日時分秒を表示する。

testuser$ cat test.csh
#!/bin/csh
echo `date +"%Y/%m/%d %H:%M:%S"`
testuser$ csh test.csh
2011/03/20 18:50:48


ファイルパスからファイル名のみを表示する。

「/home/testuser/test.txt」からファイル名の箇所のみを表示する。

testuser$ cat test.csh
#!/bin/csh
set file = $1:t
echo $file
testuser$ csh test.csh /home/testuser/test.txt
test.txt



【これがオススメ】
以下のamazonリンクから購入すると齋藤緒のページ作りパワーが劇的にアップします。


応援サイト
仕事支援sed awk
ポコパーン

inserted by FC2 system