指令碼中除了命令之外還包括以下元素:變數,使用者輸入,控制流,環境變數。
上一次用fork,execvp,wait實現了乙個能夠建立程序和執行程式的shell。此次對這個shell做一些改進。加入命令列解析,這樣使用者可以在一行中輸入命令和所有引數了,然後將控制語句if...then加入到這個shell中,最後加入區域性變數和環境變數。本次先加入第乙個功能。
第乙個改進是新增命令列解析的功能。使用者可在一行中輸入 比如: find /home -name core -mtime +3 -print
然後由解析器將命令列拆成字串陣列,以便傳給execvp。在shell中忽略訊號sigint和sigquit,但是在子程序中恢復對訊號sigint和sigquit的預設操作,允許使用者通過按表示結束檔案的ctrl-d來退出。此版本命名為smsh1.其shell的主函式如下:
int main()
free(cmdline);
} return 0;
}
其中3個函式解釋:
1.next_cmd 從輸入流讀入下乙個命令。它呼叫malloc來分配記憶體以接受任意長度的命令列。碰到檔案結束符,它返回null。
2.splitline 將乙個字串分解為字串陣列,並返回這個陣列。它呼叫malloc來分配記憶體以接受任意引數個數的命令列。此陣列以null標記結束。
3.execute 執行乙個命令,返回命令的結束狀態。
smsh1由三個檔案組成 smsh1.c ,splitline.c,execute.c
三者**如下:
smsh1.c:
#include #include #include #include #include "smsh.h"
#define dfl_prompt "> "
int main()
free(cmdline);
} return 0;
}void setup()
/* * purpose: initialize shell
* returns: nothing. calls fatal() if trouble
*/void fatal(char *s1, char *s2, int n)
splitline.c:
#include #include #include #include "smsh.h"
char * next_cmd(char *prompt, file *fp)
/* end of command? */
if ( c == '\n' )
break;
/* no, add to buffer */
buf[pos++] = c;
} if ( c == eof && pos == 0 ) /* eof and no input */
return null; /* say so */
buf[pos] = '\0';
return buf;
}#define is_delim(x) ((x)==' '||(x)=='\t')
char ** splitline(char *line)
/* mark start, then find end of word */
start = cp;
len = 1;
while (*++cp != '\0' && !(is_delim(*cp)) )
len++;
args[argnum++] = newstr(start, len);
} args[argnum] = null;
return args;}/*
* purpose: constructor for strings
* returns: a string, never null
*/char *newstr(char *s, int l)
void
freelist(char **list)
/* * purpose: free the list returned by splitline
* returns: nothing
* action: free all strings in list and then free the list
*/void * emalloc(size_t n)
void * erealloc(void *p, size_t n)
/* execute.c - code used by small shell to execute commands */
#include #include #include #include #include int execute(char *argv)
/* * purpose: run a program passing it arguments
* returns: status returned via wait, or -1 on error
* errors: -1 on fork() or wait() errors
*/ else
return child_info;
}
smsh.h:
#define yes 1
#define no 0
char *next_cmd();
char **splitline(char *);
void freelist(char **);
void *emalloc(size_t);
void *erealloc(void *, size_t);
int execute(char **);
void fatal(char *, char *, int );
int process();
編譯執行:
$ gcc smsh1.c splitline.c execute.c -o smsh1
$ ./smsh1
>ps -f
uid pid ppid c stime tty time cmd
sk 3982 3972 0 19:49 pts/0 00:00:00 bash
sk 4637 3982 0 20:11 pts/0 00:00:00 ./smsh1
sk 4638 4637 0 20:11 pts/0 00:00:00 ps -f
>這裡按ctrl-d鍵
其中ps -f是./smsh1的子程序,./smsh1是bash的子程序.
shell 2 數值運算
1 expr 用法比較麻煩很少使用。示例 jamin localhost expr 1 2 3 號兩邊必須有空格 jamin localhost expr 3 2 6 號是特殊字元。2 使用 示例 jamin localhost echo 1 2 3 jamin localhost echo 3 2...
shell 2 顯示帶顏色字型
shell指令碼中echo顯示內容帶顏色顯示,echo顯示帶顏色,需要使用引數 e,格式如下 echo e 033 字背景顏色 文字顏色m字串 033 0m 例如echo e 033 41 36m something here 033 0m 其中41的位置代表底色,36的位置是代表字的顏色 注 字型...
實現乙個簡單的shell(2)
tomorrow 星辰 部落格。這是本部落格的第乙個文章 主要介紹如何用 c 語言基於linux 系統來實現乙個簡單shell diy 乙個shell 通過自己程式設計實現乙個linux 下的shell,可以使得個人對程序的概念 程序的通訊和作業系統的執行的理解更加的深刻。還會大大增加個人學習的成就...