我的第乙個shell指令碼

2021-07-12 01:53:57 字數 1225 閱讀 8927

今天去某公司面試,做了一道這樣的筆試題。

題目:假如日誌檔案有5種型別(error, debug, info, exception, ver),怎樣寫乙個指令碼讓他們歸類統計錯誤型別的次數?

error: abd……

debug: ggsgfggfgfggf

…… ……

info: 13445jflkjfllalss

鑑於昨天只看了一天的shell指令碼的編寫,當時寫出來的不敢保證正確,今天回來試了下,居然能執行,哈哈,不過沒有考慮成千上萬行的優化問題,這個需要繼續學習,把今天的**貼在下面

#!/bin/bash

# program:

# get the log.txt different type error number

# history

# 2016-05-25 xiaoxiaoxi first realse

path=/bin:/sbin

:/usr/bin

:/usr/sbin

:/usr/local/bin

:/usr/local/sbin

:~/bin

export path

counterror=`cat log.txt | grep error | wc -l`

countdebug=`cat log.txt | grep debug | wc -l`

countinfo=`cat log.txt | grep info | wc -l`

countexception=`cat log.txt | grep exception | wc -l`

countver=`cat log.txt | grep ver | wc -l`

echo "count error="

$counterror

echo "count debug="

$countdebug

echo "count info="

$countinfo

echo "count exception="

$countexception

echo "count ver="

$countver

我這裡的log.txt是放在當前shell指令碼的資料夾下的,如果我們的log.txt不在當前資料夾下,可以將log.txt表示成絕對路徑,經試驗也是可行的。

其他的優化的和改進的暫時還沒有想到。。。。。。。。。

第乙個Shell指令碼

開啟文字編輯器,新建乙個檔案,擴充套件名為sh sh代表shell 副檔名並不影響指令碼執行,見名知意就好,如果你用php寫shell 指令碼,副檔名就用php好了。輸入一些 bin bash echo hello world 是乙個約定的標記,它告訴系統這個指令碼需要什麼直譯器來執行,即使用哪一種...

第乙個shell指令碼

首先宣告,我的linux系統是centeros 64bit的 開啟終端,cd mnt hgfs share 進入以下目錄 mkdir shellfolder 建立資料夾shellfolder touch runstusys.sh 建立乙個shell程式 vi runstusys.sh 進行編輯,並輸...

第乙個shell指令碼

hostname term hello shell histsize ssh client world cvs rsh path 通過shell讀取文字檔案,並到系統變數中查詢是否存在 shell命令如下 bin sh filename env var.txt num cat filename wc...