用C语言编写一个Linux下的简单shell程序

作者在 2010-11-27 00:49:37 发布以下内容
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>

char parsecmd(char *);
/************关于本文档********************************************
*filename:用C语言编写一个Linux下的简单shell程序
*purpose:展示了C语言如何进行系统调用执行logout cd ls pwd pid rm mkdir mv cp等命令
*wrote by: zhoulifa(zhoulifa@163.com) 周立发(
http://zhoulifa.bokee.com)
Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言编程
*date time:2006-07-16 15:00:00
*Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途
* 但请遵循GPL。
*Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力
********************************************************************
*/
int main(int argc, char ** argv)
{
char cmd[1024];
char tmp[101];
char b = 'F';
char * p;

while(1) {
    memset(cmd, 0, 1024);
    printf("Next command%% ");/*显示提示信息*/
    fgets(cmd, 1000, stdin);/*接受用户输入,命令最长1000个字符*/
    cmd[strlen(cmd) - 1] = 0;/*去掉输入的回车符*/

    b = parsecmd(cmd);/*分析取得用户命令字*/

    if(b=='T') break;/*如果是logout就退出程序*/

    switch(b){
    case 1:/*切换工作目录*/
      if(chdir(cmd + 3) != 0) {
        printf("chdir(%s) error!%s\n", cmd + 3, strerror(errno));
      }
      printf("I'm working in '%s' now\n", getcwd(tmp, 100));
      break;
    case 2:/*ls命令*/
    case 7:/*cp命令*/
    case 8:/*pwd命令*/
      system(cmd);
      break;
    case 3:/*pid命令*/
      printf("%d\n", getpid());
      break;
    case 4:/*rm命令*/
      remove(cmd + 3);
      break;
    case 5:/*mkdir命令*/
      mkdir(cmd + 6, 0755);
      break;
    case 6:/*mv命令*/
      p = strchr(cmd + 3, ' ');
      *p = 0;
      rename(cmd + 3, p + 1);
      break;
    case 0:/*不能识别的命令*/
      printf("Bad command, try again! All aviable commands are:\nlogout cd ls pwd pid rm mkdir mv cp\n", getpid());
      break;
    }
}
}

char parsecmd(char * s)
{
if(!strcasecmp(s, "logout")) return 'T';
else if(!strncasecmp(s, "cd", 2)) return 1;
else if(!strncasecmp(s, "ls", 2)) return 2;
else if(!strncasecmp(s, "pwd", 3)) return 8;
else if(!strncasecmp(s, "pid", 3)) return 3;
else if(!strncasecmp(s, "rm", 2)) return 4;
else if(!strncasecmp(s, "mkdir", 5)) return 5;
else if(!strncasecmp(s, "mv", 2)) return 6;
else if(!strncasecmp(s, "cp", 2)) return 7;
else return 0;
}

命令运行结下:

Next command% ls /
bin   cdrom dev home    initrd.img lost+found mnt proc sbin sys usr vmlinuz
boot data   etc initrd lib         media       opt root srv   tmp var
Next command% pwd
/data/example/c
Next command% cd /
I'm working in '/' now
Next command% pwd
/
Next command% cd /tmp
I'm working in '/tmp' now
Next command% pwd
/tmp
Next command% ls
gconfd-administrator ksocket-administrator plugtmp                                   ssh-MmlTLh5149
gconfd-root           mapping-administrator scim-helper-manager-socket-administrator virtual-administrator.XSmKSv
kde-administrator     orbit-administrator    scim-panel-socket:1-administrator
keyring-g5rmCS        orbit-root             scim-socket-frontend-administrator
Next command% mkdir xxxx
Next command% ls
gconfd-administrator ksocket-administrator plugtmp                                   ssh-MmlTLh5149
gconfd-root           mapping-administrator scim-helper-manager-socket-administrator virtual-administrator.XSmKSv
kde-administrator     orbit-administrator    scim-panel-socket:1-administrator         xxxx
keyring-g5rmCS        orbit-root             scim-socket-frontend-administrator
Next command% rm xxxx
Next command% ls
gconfd-administrator ksocket-administrator plugtmp                                   ssh-MmlTLh5149
gconfd-root           mapping-administrator scim-helper-manager-socket-administrator virtual-administrator.XSmKSv
kde-administrator     orbit-administrator    scim-panel-socket:1-administrator
keyring-g5rmCS        orbit-root             scim-socket-frontend-administrator
?

Next command% logout

技术 | 阅读 3096 次
文章评论,共3条
wtt88
2011-01-22 21:32
1
我正在学习C语言,我相信在不久将来我会看得懂!
vfdff(作者)
2011-11-25 21:27
2
# BEGIN DRIVER FRAMEWORK<br />
initialize <br />
<br />
argv=&quot;$@&quot;<br />
for opt in ${argv}; do&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp; echo opt=&quot;$opt&quot; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; if [[ ${opt:0:1} == &quot;@&quot; ]];then<br />
&nbsp; &nbsp;&nbsp; &nbsp; aa=`cat ${opt:1}`<br />
&nbsp; &nbsp;&nbsp; &nbsp; echo aa---------------=$opt<br />
&nbsp; &nbsp;&nbsp; &nbsp; echo =====aa=$aa<br />
&nbsp; &nbsp;&nbsp; &nbsp; optfiles=&quot;$optfiles $aa&quot;<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp;&nbsp; &nbsp; optfiles=&quot;$optfiles $opt&quot;<br />
&nbsp; &nbsp; fi<br />
done <br />
<br />
echo optfiles=&quot;$optfiles&quot;<br />
exit<br />
<br />
handle_opt &quot;$@&quot;
vfdff(作者)
2011-11-26 01:05
3
The shell sets some environment variables according to the command line arguments specified:<br />
<br />
$0 The name the script was invoked with. This may be a basename without directory component, or a path name. This variable is not changed with subsequent shift commands.<br />
 <br />
$1, $2, $3, ... The first, second, third, ... command line argument, respectively. The argument may contain whitespace if the argument was quoted, i.e. &quot;two words&quot;.<br />
 <br />
$# Number of command line arguments, not counting the invocation name $0<br />
 <br />
$@ &quot;$@&quot; is replaced with all command line arguments, enclosed in quotes, i.e. &quot;one&quot;, &quot;two three&quot;, &quot;four&quot;. Whitespace within an argument is preserved.<br />
 <br />
$* $* is replaced with all command line arguments. Whitespace is not preserved, i.e. &quot;one&quot;, &quot;two three&quot;, &quot;four&quot; would be changed to &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;.<br />
This variable is not used very often, &quot;$@&quot; is the normal case, because it leaves the arguments unchanged.
游客请输入验证码
浏览1940947次