无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 2900|回复: 8
打印 上一主题 下一主题

[教程] Linux: What is Dash ( /bin/dash ) Shell?

[复制链接]
跳转到指定楼层
1#
发表于 2018-1-6 09:47:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 不点 于 2018-1-6 10:04 编辑

Linux: What is Dash ( /bin/dash ) Shell?


https://www.cyberciti.biz/faq/debian-ubuntu-linux-binbash-vs-bindash-vs-binshshell/


May 28, 2011in Categories BASH Shell, Debian / Ubuntu last updated May 28, 2011

What is /bin/dash? I noticed that the default system shell /bin/sh was changed to /bin/dash. Why was this change made?

Dash is an acronym for Debian Almquist shell (dash). It is a Unix and Linux shell which is much smaller than bash but still aiming at POSIX-compliancy. dash is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. dash is a direct descendant of the NetBSD version of ash (the Almquist SHell), ported to Linux in early 1997. It was renamed to dash in 2002.

From the dash man page:

    dash is the standard command interpreter for the Linux system. The current version of dash is in the process of being changed to conform with the POSIX 1003.2 and 1003.2a specifications for the shell. This version has many features which make it appear similar in some respects to the Korn shell, but it is not a Korn shell clone. Only features designated by POSIX, plus a few Berkeley extensions, are being incorporated into this shell.

Starting with DebianSqueeze and Ubuntu 6.10, the default shell will be dash. The default system shell, /bin/sh, was changed to /bin/dash due to the following technical reasons:

    To speed up the system boot time. The reason is that dash starts faster than bash, and the shell is started quite a lot of times during boot. Measurements showed that they run equally fast, so the improved boot time must be due to dash doing less work during initialization.
    It requires less disk space but is also less feature-rich.
    It depends on fewer libraries.
    It is believed to be more reliable in the case of upgrade problems or disk failures.

A Note About Bash Specific Scripts

It is recommend that to avoid errors with your own Bash-specific scripts you can use the shebang line as follows:
#!/usr/bin/env bash.

OR
#!/path/to/real/bash.binary

OR
#!/usr/local/bin/bash

However, Ubuntu wiki recommends that:

    Developers of shell scripts adhere to the POSIX standard, omitting those items flagged as XSI extensions. Doing so will improve portability to a variety of Unix systems, and will provide assurance that problems you encounter will be treated as bugs rather than as undocumented features.

Test all your scripts and see if they will break once /bin/sh defaults to /bin/dash using virtualization or test machine.
References:

    Dash page from the Debian wiki.
    Dash as /bin/sh page from the Ubuntu wiki.
    Proposed release goal: Switch to dash as /bin/sh to speed up the boot.
    man dash

project page http://gondor.apana.org.au/~herbert/dash/

git repository http://git.kernel.org/?p=utils/dash/dash.git;a=summary

files http://gondor.apana.org.au/~herbert/dash/files/

推荐
 楼主| 发表于 2018-1-9 13:35:13 | 只看该作者
谢谢您提供见解。我是被 zsh 的代码搅糊涂了,才转到 bash,然而 bash 的代码也还是太乱,折腾了一两个月,最终发现还是折腾不动,彻底服气了。所以,被迫来折腾一个没有什么功能的、纯粹的 shell 软件。

我已经下载了 dash,它的代码果然很简练,至少比 bash 简练多了。感觉这可能是适合我的那种软件。
回复

使用道具 举报

2#
发表于 2018-1-8 21:19:42 | 只看该作者
之前看过DASH源码,应该是从FreeBSD里面提取出来的。

点评

谢谢。请谈谈 dash 的源码,它是否容易阅读和理解? bash 的源码太乱了,我是已经受不了了,才被迫转向 dash。 我还没开始下载 dash。 我是看到 debian 和 ubuntu 采用 dash 来取代 bash,所以才想到应该关  详情 回复 发表于 2018-1-8 22:12
回复

使用道具 举报

3#
 楼主| 发表于 2018-1-8 22:12:46 | 只看该作者
njlyf2011 发表于 2018-1-8 21:19
之前看过DASH源码,应该是从FreeBSD里面提取出来的。

谢谢。请谈谈 dash 的源码,它是否容易阅读和理解?

bash 的源码太乱了,我是已经受不了了,才被迫转向 dash。

我还没开始下载 dash。

我是看到 debian 和 ubuntu 采用 dash 来取代 bash,所以才想到应该关注 dash 的。

点评

那个……不好意思……我目前不会编程所以代码并不能看懂……  详情 回复 发表于 2018-1-9 12:45
回复

使用道具 举报

4#
发表于 2018-1-9 12:45:04 | 只看该作者
不点 发表于 2018-1-8 22:12
谢谢。请谈谈 dash 的源码,它是否容易阅读和理解?

bash 的源码太乱了,我是已经受不了了,才被迫转 ...

那个……不好意思……我目前不会编程所以代码并不能看懂……
回复

使用道具 举报

5#
发表于 2018-1-9 12:46:20 来自手机 | 只看该作者
另外如果您不想用bash的话,试试zsh之类的吧……dash功能实在不多,连自动补全也没有
回复

使用道具 举报

7#
 楼主| 发表于 2018-2-15 19:15:18 | 只看该作者
本帖最后由 不点 于 2018-2-20 11:35 编辑

费了九牛二虎之力,终于让 dash 能够用 g++ 编译了。

dash.zip

158.08 KB, 下载次数: 0, 下载积分: 无忧币 -2

能用 gcc 和 g++ 编译了。

dash.rar

110.29 KB, 下载次数: 0, 下载积分: 无忧币 -2

又删减了一些代码,更小一点了。

回复

使用道具 举报

8#
 楼主| 发表于 2018-2-18 20:17:18 | 只看该作者
找到一个最小的 shell,感觉很有启发性,适合入门。

http://bbs.21ic.com/icview-1614664-1-1.html
http://www.samiam.org/software/yash.html

Yash: Yet another shell
This is a simple thought experiment: How small can we make a usable*NIX command shell?  This program, only 127 lines long (complete withdocumentation), is a very basic *NIX command shell.  It can be downloaded here
Since the program is so small, I am including its source below.  Note that there are ways to make this smaller by, say, using strtok to parse the line, and strcmp to handle the "cd" command.  However, this code style makes extending the shell easier; the smallest shell that one can make is actually
  1. main(){char i[99];for(;;){gets(i);system(i);}}
复制代码


Without further ado, the code:

  1. /* Placed in the public domain 2009 by Sam Trenholme */

  2. /* Yash: A usable command shell in under 4k (yet another shell)
  3. *
  4. * This is a simple thought experiment: How small can we make a *NIX command
  5. * line shell?  This is as simple as it gets; it will run a command typed
  6. * in if it's an external command like "ls" or "cc", with arguments separated
  7. * by space; if one types in "-cd {directory}", it will go to that directory.
  8. *
  9. * CNTL-C exits the shell
  10. *
  11. * BUGS: CNTL-D reruns the previous command instead of exiting the shell.
  12. */

  13. #define LINEMAX 80
  14. #define ARGMAX 16

  15. #include <stdio.h>
  16. #include <unistd.h>

  17. /* Given a pointer to arguments, destroy the string */
  18. void yash_zap_args(char **zap) {
  19.         int a = 0;

  20.         if(zap == 0) {
  21.                 return;
  22.         }

  23.         for(a = 0; a < ARGMAX; a++) {
  24.                 if(zap[a] != 0) {
  25.                         free(zap[a]);
  26.                         zap[a] = 0;
  27.                 }
  28.         }

  29.         free(zap);
  30. }

  31. /* Given a line separated by whitespace, return an array of strings
  32. * of the individual arguments */

  33. char **yash_args(char *in) {
  34.         char **out = 0;
  35.         int a = 0, b = 0;

  36.         /* Sanity checks */
  37.         if(in == 0) {
  38.                 return 0;
  39.         }

  40.         out = malloc(ARGMAX * sizeof(char *));
  41.         for(a = 0; a < ARGMAX; a++) {
  42.                 out[a] = 0;
  43.         }
  44.         a = 0;

  45.         if(out == 0) {
  46.                 return 0;
  47.         }

  48.         while(*in != 0) {
  49.                 if(*in != ' ' && *in != '\n') {
  50.                         if((out[a] = malloc(LINEMAX)) == 0) {
  51.                                 goto catch_yash_args;
  52.                         }
  53.                         b = 0;
  54.                         while(b < LINEMAX - 2 && *in != ' ' && *in != 0 &&
  55.                               *in != '\n') {
  56.                                 out[a][b] = *in;
  57.                                 b++;
  58.                                 in++;
  59.                         }
  60.                         out[a][b] = 0;
  61.                         a++;
  62.                         if(a >= ARGMAX) {
  63.                                 goto catch_yash_args;
  64.                         }
  65.                 }
  66.                 in++;
  67.         }       

  68.         return out;
  69.                
  70. catch_yash_args:
  71.         yash_zap_args(out);
  72.         return 0;
  73. }

  74. /* Meta commands for yash; right now this is just the cd command */
  75. void yash_meta(char **args) {
  76.         if(args == 0 || args[0] == 0) {
  77.                 return;
  78.         }

  79.         if(args[0][1] == 'c' && args[0][2] == 'd') {
  80.                 if(args[1] == 0) {
  81.                         chdir("/"); /* Should be $HOME, isn't */
  82.                 } else {
  83.                         chdir(args[1]);
  84.                 }
  85.                 return;
  86.         }
  87. }
  88.                
  89. main() {
  90.         char in[LINEMAX], **args = 0;

  91.         for(;;) {
  92.                 printf("$ ");
  93.                 fgets(in,LINEMAX,stdin);
  94.                 args = yash_args(in);
  95.                 if(args != 0 && args[0] != 0) {
  96.                         if(*args[0] == '-') {
  97.                                 yash_meta(args);
  98.                         }
  99.                         else if(fork()) {
  100.                                 wait(0);
  101.                         } else {
  102.                                 execvp(args[0],args);
  103.                                 printf("Command not found\n");
  104.                                 exit(-1);
  105.                         }
  106.                 }               
  107.                 yash_zap_args(args);
  108.         }
  109. }
复制代码


回复

使用道具 举报

9#
 楼主| 发表于 2018-2-19 09:08:44 | 只看该作者
本帖最后由 不点 于 2018-2-19 11:56 编辑

上述最小 shell —— yash 确实很棒,入门级最佳途径。

下面是另外一个 shell —— msh,只有 5K:

http://ibiblio.org/pub/linux/system/shells/!INDEX.html


  1. /* Minimal shell C source - (c) 1999, Spock (Oscar Portela Arjona) */

  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include <stdio.h>

  9. #define chkerr(c,msg) if (c < 0) {perror("ERROR (" msg ")"); exit(-1);}
  10. #define mvdesc(d1,d2) {close(d1); dup(d2); close(d2);}
  11. #define redir(n,f)    {close(n); chkerr(open(fv[n],f,0666),"open");}
  12. #define size(v,s,u,n,t) {(v = (t)realloc(v,s))[u] = n;}
  13. #define l2            l1[vc]
  14. #define l3            l2[p[0]]

  15. int main(void) {
  16.     char ***l1 = NULL, *fv[3] ,dir[50] ,c;
  17.     int vc, bg, id, p[2], d;
  18.     void *ttt = 0;
  19.     ttt = ttt;

  20.     signal(SIGINT, SIG_IGN);       
  21.     signal(SIGQUIT,SIG_IGN);

  22.     while (1) {
  23.         getcwd(dir,50);
  24.         write(1,dir,strlen(dir));
  25.         write(1," $ ",d = bg = 3);
  26.         for (;bg; fv[bg] = NULL)
  27.             ttt=realloc(fv[--bg],0);
  28.         size(l1,4,0,NULL,char ***);
  29.         for (vc = p[0] = 0; read(0,&c,1) && (c != '\n');)
  30.             switch(c) {
  31.                 case '<':
  32.                     d = 0; break;
  33.                 case '>':
  34.                     d = 1; break;
  35.                 case '|':
  36.                     if (l2) {
  37.                         vc++;
  38.                         p[0] = 0;
  39.                     }
  40.                     d = 3; break;
  41.                 case '&':
  42.                     if (d < 3)
  43.                         d++;
  44.                     else
  45.                         bg = 1; break;
  46.                 case ' ':
  47.                     if (d < 3) {
  48.                         if (fv[d])
  49.                             d = 3;
  50.                     } else if (l2 && l3)
  51.                         p[0]++; break;
  52.                 default:  
  53.                     if (d < 3) {
  54.                         if (!fv[d])
  55.                             size(fv[d],1,0,'\0',char *);
  56.                         id=strlen(fv[d]);
  57.                         size(fv[d],id+2,id,c,char *);
  58.                         fv[d][id+1]='\0';
  59.                     }else {
  60.                         if (!l2) {
  61.                             size(l1,vc*4+8,vc+1,NULL,char ***);
  62.                             size(l2,4,0,NULL, char **);
  63.                         }
  64.                         if (!l3) {
  65.                             size(l2,p[0]*4+8,p[0]+1,NULL,char **);
  66.                             size(l3,1,0,'\0', char *);
  67.                         }
  68.                         id=strlen(l3);
  69.                         size(l3,id+2,id,c, char *);
  70.                         l3[id+1] = '\0';
  71.                     }
  72.             }
  73.         for (vc = 0; l2;) {
  74.             if (!vc)
  75.                 d = dup(0);
  76.             if (l1[vc+1])
  77.                 chkerr(pipe(p),"pipe");
  78.             if (!strcmp(l2[0],"exit"))
  79.                 exit(0);
  80.             if (!strcmp(l2[0],"cd")) {
  81.                 if (chdir(l2[1]) < 0)
  82.                     chdir(getenv("HOME"));
  83.             } else {
  84.                 if (!(id = fork())) {
  85.                         if (fv[0] && !vc)
  86.                             redir(0,O_RDONLY)
  87.                         else
  88.                             mvdesc(0,d);
  89.                         if (fv[1])
  90.                             redir(1,O_CREAT|O_WRONLY|O_TRUNC);
  91.                         if (fv[2])
  92.                             redir(2,O_CREAT|O_WRONLY|O_TRUNC);
  93.                         if (l1[vc+1]) {
  94.                             mvdesc(1,p[1]);
  95.                             close(p[0]);
  96.                         }
  97.                         if (!bg) {
  98.                             signal(SIGINT,SIG_DFL);
  99.                             signal(SIGQUIT,SIG_DFL);
  100.                         }
  101.                         chkerr(execvp(l2[0],l2),"exec");
  102.                     }
  103.                 if (!l1[vc+1] && !bg)
  104.                     while (wait(NULL) != id);
  105.             }
  106.             for (id = 0; l2[id]; ttt = realloc(l2[id++],0));
  107.             ttt = realloc(l2,0);
  108.             close(d);
  109.             if (l1[++vc]) {
  110.                 d = dup(p[0]);
  111.                 close(p[0]);
  112.                 close(p[1]);
  113.             }
  114.         } //for (vc = 0; l2;)
  115.     } //while (1)
  116. }
复制代码



下面这个叫做 “s” 的 shell,还在活跃开发中:

https://www.reddit.com/r/tinycode/comments/64m8lc/s_minimal_shell/


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-12-10 19:47

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表