{"msg":"操作成功","code":200,"data":{"createBy":"admin","createTime":"2021-01-14 20:26:27","updateBy":"admin","updateTime":"2025-06-16 20:16:37","remark":null,"id":42,"articleTitle":"Shell（一）Shell概述","articleUrl":"shell_introduction","articleThumbnail":"  https://www.asumimoe.com/imgfiles/20220908/7769fde9ec5445068e41583cb1c71894.jpg","articleFlag":"0","draftStatus":"1","reprintStatement":"1","articleSummary":"Shell是一个命令行解释器，它接收应用程序/用户命令，然后调用操作系统内核。Shell还是一种功能强大的编程语言，易编写、易调试、灵活性强。","articleContent":"## Shell概述\n\nShell是一个命令行解释器，它接收应用程序/用户命令，然后调用操作系统内核。Shell还是一种功能强大的编程语言，易编写、易调试、灵活性强。\n\n1）Linux提供的Shell解析器有\n\n```bash\n[root@l1 ~]# cat /etc/shells \n/bin/sh\n/bin/bash\n/sbin/nologin\n/usr/bin/sh\n/usr/bin/bash\n/usr/sbin/nologin\n/bin/tcsh\n/bin/csh\n```\n\n2）bash和sh的关系\n\n```bash\n[root@l1 ~]# ll /bin/sh\nlrwxrwxrwx. 1 root root 4 7月  24 2021 /bin/sh -> bash\n```\n\n## Shell脚本入门\n\n1. shell脚本通常以`#!/bin/bash`开始\n\n2. 案例演示：在界面中输出helloworld\n\n   ```bash\n   #编写脚本\n   [root@l1 scripts]# cat hello.sh \n   #!/bin/bash\n   echo \"helloword!!!\"\n   ```\n\n3. 脚本常用的执行方式\n\n   1）采用bash或sh+脚本的相对路径或绝对路径执行\n\n   ```bash\n   [root@l1 scripts]# sh ./hello.sh\n   helloword!!!\n   [root@l1 scripts]# sh /root/scripts/hello.sh \n   helloword!!!\n   [root@l1 scripts]# bash ./hello.sh \n   helloword!!!\n   [root@l1 scripts]# bash /root/scripts/hello.sh \n   helloword!!!\n   ```\n\n   2）采用输入脚本的绝对路径或者相对路径来执行脚本（必须要有执行权限）\n\n   ```bash\n   #为脚本赋予执行权限\n   [root@l1 scripts]# ll\n   -rw-r--r-- 1 root root 32 9月  10 20:31 hello.sh\n   [root@l1 scripts]# chmod +x hello.sh\n   [root@l1 scripts]# ll\n   -rwxr-xr-x 1 root root 32 9月  10 20:31 hello.sh\n   [root@l1 scripts]# \n   #执行脚本\n   [root@l1 scripts]# ./hello.sh \n   helloword!!!\n   [root@l1 scripts]# /root/scripts/hello.sh \n   helloword!!!\n   ```\n\n   3）在脚本前加. 或者source也可以执行脚本（相对路径，绝对路径均可）\n\n   ```bash\n   [root@l1 scripts]# . hello.sh (注意中间有空格)\n   helloword!!!\n   [root@l1 scripts]# source hello.sh \n   helloword!!!\n   ```\n\n   注：第一种与第二种方式会开辟一个子shell来执行脚本中的命令，当脚本内容结束，则子shell关闭回到父shell中；而第三种方式是直接在当前shell中执行。\n\n   可以在命令行中执行bash命令来开辟一个子shell\n\n   ```bash\n   [root@l1 scripts]# ps -f | grep bash\n   root       9315   9313  0 20:18 pts/0    00:00:00 -bash\n   root       9716   9315  0 20:54 pts/0    00:00:00 grep --color=auto bash\n   [root@l1 scripts]# bash\n   [root@l1 scripts]# ps -f | grep bash\n   root       9315   9313  0 20:18 pts/0    00:00:00 -bash\n   root       9725   9315  0 20:54 pts/0    00:00:00 bash\n   root       9761   9725  0 20:54 pts/0    00:00:00 grep --color=auto bash\n   [root@l1 scripts]# exit\n   exit\n   ```","categoryId":5,"viewCount":550,"categoryName":"Shell","author":"球接子","authorAvatar":null,"tagIds":[4,10],"tagNames":["Shell","Linux基础"]}}