1. 程式人生 > >再次嘮叨linux中的strings命令

再次嘮叨linux中的strings命令

        以前我用strings命令的頻率不高, 現在越用越順手, 而且已經是離不開strings命令了。雖然以前說過strings命令, 但今天還是要說。 主要是兩大用途, 下面來說明一下:

        一. 確認程式碼編是否譯到庫中去了?

        在大型的開發中, 我們經常修改一兩行程式碼, 有時候自己修改的程式碼並沒有編到庫中去(有可能是程式碼沒有同步到linux上編譯, 也有可能是有編譯錯誤導致生成庫失敗, 也有肯能是該make clean的地方沒有make clean或rm庫, 還有可能是其他雜七雜八的原因), 這樣會經常誤導自己的判斷, 折騰大半天。

        還有, 比如你程式碼提交了, 結果你的同事更新(svn up)了程式碼(也就是獲取了你的程式碼), 但編譯的時候由於各種原因沒有把你的程式碼編譯進去, 然後他提交了這個庫, 然後呢, 自然就出了問題, 老闆找到你的頭上。 此時, 可以用strings命令快速判斷提交的庫中是否包含你自己的程式碼。 我用過無數次, 屢試不爽。 說了這麼多, 下面來實戰一下:

[[email protected] test]$ ls
test.c
[[email protected] test]$ cat test.c 
#include <stdio.h>

int main()
{
        int a = 1;
        int b = 2;
        int c = a + b;
        printf("xxx, %d, %d, %d\n", a, b, c);


        return 0;
}
[[email protected] test]$ gcc test.c 
[[email protected]
test]$ ls a.out test.c [[email protected] test]$ strings a.out | grep xxx xxx, %d, %d, %d [[email protected] test]$
       如上, 一般都是在自己寫的程式碼中, 加點日誌(字串), 相當於打點tag, 然後用strings命令進行判斷。 如果是不需要加日誌, 那怎麼辦呢? 我經常是對檔案中的之前日誌中的字串做簡要修改, 比如加上xxx, 實際上也相當於打下自己的tag印記。

        二. 對於新手來說, 需要知道新生成的程式碼進入了哪個庫, 然後才好找庫啊!

        這個需求也很合理, 不能說程式碼編譯到哪個庫中去了, 你還不知道。 用strings命令吧, 如下:

[[email protected] test]$ find -name "*" | xargs strings -f | grep xxx
./test.c: 	printf("xxx, %d, %d, %d\n", a, b, c);
./a.out: xxx, %d, %d, %d
[[email protected] test]$ 
         OK,  一目瞭然, 知道xxx編譯到哪裡去了。要補充一下, find命令中的雙引號不能少, 否則:
[[email protected] test]$ find -name * 
find: paths must precede expression: test.c
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[[email protected] test]$ 

       

        OK, 天天用的東西, 多說一次也無妨。