shell的基本知识

shell
1、什么叫shell
在linux内核与用户之间的解释器程序
通常指/bin/bash
负责向内核翻译及传达用户、程序指令
相当于操作系统的“外壳”、
2、shell的使用方式
交互式-------命令行
非交互式-----脚本


硬件----系统软件【内核】----应用软件----人
shell, bash, sh, ksh

shell[命令解释器]
[root@vh01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
[root@vh01 ~]# echo $SHELL
/bin/bash
[root@vh01 ~]# yum -y install ksh
[root@vh01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
/bin/ksh

命令和路径
选项

/etc/profile   环境变量和启动程序
/etc/bashrc 函数和别名
/root/.bash_profile
/root/.bashrc

vim /etc/profile
HISTSIZE=1000  命令历史的条目

#!/bin/bash
for i in {1..10}
do
     useradd user$i  2>> uerror.log >/dev/null
     echo "123"|passwd --stdin user$i  2>>perror.log  >/dev/null
done

标准输出(1),错误输出(2)
[root@vh01 ~]# ls install.log > log
[root@vh01 ~]# ls tabasfd  > log
ls: 无法访问tabasfd: 没有那个文件或目录
[root@vh01 ~]# ls install.log abc >log1 2>log2
[root@vh01 ~]# ls install.log abc &>logs
[root@vh01 ~]# ls install.log abc >mylog 2>&1
[root@vh01 ~]# ls install.log 2>> logs
install.log
[root@vh01 ~]# ls install.log > /dev/null
[root@vh01 ~]# mail -s "test" root </etc/passwd



常见的脚本语言
Bash Shell
Python/Perl/Ruby
JSP/PHP/ASP/CGI
JavaScript

脚本语言
写代码,找解释器执行代码,输入结果

C语言 [系统,应用软件]
写代码,编译代码,执行代码
源码-----二进制.      exe


脚本三步走
1、新建文件
2、添加可执行的脚本语句(命令行)
3、添加x执行权限

vim a.sh
#!/bin/bash
echo "Hello World"

一、需要权限

[root@vh01 ~]# chmod +x a.sh
[root@vh01 ~]# ./a.sh
[root@vh01 ~]# /var/tmp/a.sh
[root@vh01 ~]# /root/a.sh

二、不需要权限
开子进程
[root@vh01 ~]# bash a.sh
[root@vh01 ~]# sh a.sh
不开子进程
[root@vh01 ~]#source a.sh
[root@vh01 ~]#.  a.sh
[root@vh01 ~]#

vim yum.sh
#!/bin/bash
echo '[rhel-packages]
name=Redhat Enterprise Linux6
baseurl=ftp://192.168.4.254/rhel6/Server
enable=1
gpgcheck=0'> /etc/yum.repos.d/yum.repo

posted @ 2021-08-08 11:38  Linux刀客  阅读(70)  评论(0编辑  收藏  举报