标签:etc 根据 英文字母 条件 编程 引号 选项 后缀 bash
[oracle@oracle ~]$ echo $SHELL
/bin/bash
[oracle@oracle ~]$
Shell脚本编程 | |||
Shell基础 变量的使用 算术运算、数组应用 排序、提取、组合命令 | 条件测试 if语句、for循环、while循环、case分支和函数应用 | 正则表达式 expect dialog | sed Awk 案例 |
第1-2章 | 第3-4章 | 第5-6章 | 第7-8章 |
[root@oracle ~]# cat /etc/shells
/bin/sh ---》最小化安装都有
/bin/bash ---》推荐使用
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
/bin/ksh
[root@oracle ~]#
env 查看环境变量
set 查看当前用户可以使用的所有的变量
[root@oracle ~]# history
1 cd /etc
2 ls
3 cd sysconfig
4 ls
5 cd network-scripts
6 ls
[root@oracle ~]# !2
ls
anaconda-ks.cfg Documents install.log Music Public tmp
Desktop Downloads install.log.syslog Pictures Templates Videos
[root@oracle ~]# !cd
cd /backup
[root@oracle backup]#
[root@oracle ~]# vi /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
HISTSIZE=1000
[root@oracle ~]# alias
alias cp=‘cp -i‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@oracle ~]#
[root@localhost ~]# vi first.sh
cd /boot/
pwd
ls -lh vml*
[root@localhost ~]# chmod +x first.sh
[root@localhost ~]# ls -l first.sh
-rwxr-xr-x 1 root root 144 04-26 15:02 first.sh
====
声明:以#!开头
注释:以#开头
#!/bin/bash ---》申明使用bash作为默认shell来执行脚本
#########################################
#Function: auto fdisk
#Usage: bash auto_fdisk.sh
#Author: Customer service department
#Company: Alibaba Cloud Computing
#Version: 2.0
#########################################
===========
lftp 172.16.1.1:~> ls
-rwxr-xr-x 1 0 0 17921378 Sep 09 18:19 PotPlayer视频播放器.zip
drwxr-xr-x 8 0 0 4096 Nov 20 19:10 linux-video
drwxr-xr-x 2 0 0 4096 Feb 29 13:46 mysql
drwxr-xr-x 4 0 0 4096 Sep 12 20:42 python
drwxr-xr-x 7 0 0 4096 Mar 04 16:16 software
drwxr-xr-x 4 0 0 4096 Jan 23 15:10 teacher_feng
drwxr-xr-x 7 0 0 4096 Jan 05 13:44 video
drwxr-xr-x 2 0 0 4096 Dec 13 22:56 xiang
lftp 172.16.1.1:/> cd teacher_feng/
lftp 172.16.1.1:/teacher_feng> ls
drwxr-xr-x 10 0 0 4096 Dec 21 15:16 9_class
drwxr-xr-x 10 0 0 4096 Mar 05 14:05 califeng
-rw-r--r-- 1 0 0 13990 Oct 31 23:56 mysql_plan.xlsx
lftp 172.16.1.1:/teacher_feng> cd 9_class/linux-shell-course/
lftp 172.16.1.1:/teacher_feng/9_class/linux-shell-course> get yuli_admin_system_user-v2.sh
6886 bytes transferred
lftp 172.16.1.1:/teacher_feng/9_class/linux-shell-course>
[root@chinaitsoft lianxi]# lftp 172.16.1.1
lftp 172.16.1.1:~> bye
[root@chinaitsoft lianxi]# lftp 172.16.1.1
lftp 172.16.1.1:~> exit
[root@chinaitsoft lianxi]# lftp 172.16.1.1
lftp 172.16.1.1:~> quit
[root@chinaitsoft lianxi]#
====
get是下载文件
mirror 下载文件夹
退出 quit bye exit
默认情况下下载的文件或者文件夹存放在当前目录
lftp 172.16.1.1:/> !ls 在本机上执行ls命令
aa frist.sh sed yuli_admin_system_user-v2.sh
lftp 172.16.1.1:/>
lftp 172.16.1.1:/> !pwd
/lianxi
lftp 172.16.1.1:/> lcd /root 切换本地路径 local
lcd 成功, 本地目录=/root
lftp 172.16.1.1:/>
====
[root@oracle ~]# day=sunday
[root@oracle ~]# echo $day
sunday
[root@oracle ~]# DAY=sun
[root@oracle ~]# echo $day
sunday
[root@oracle ~]# echo $DAY
sun
[root@oracle ~]#
[root@oracle ~]# name="hai lian tian"
[root@oracle ~]# echo $name
hai lian tian
[root@oracle ~]#
[root@oracle ~]# DAY=Sunday
[root@oracle ~]# echo "Today is Sunday" >$DAY_file.txt
[root@oracle ~]# ls -a
.txt
[root@oracle tmp]# echo "today is sunday " >${DAY}_file.txt
[root@oracle tmp]# ls -a
. .. .esd-0 .ICE-unix Sunday_file.txt .X0-lock .X11-unix
[root@oracle tmp]#
var2=$(rpm -qf $(which fdisk))
[root@oracle tmp]# var2=$(rpm -qf $(which fdisk))
[root@oracle tmp]# echo $var2
util-linux-ng-2.17.2-12.14.el6.x86_64
[root@oracle tmp]#
[root@oracle tmp]# name="hello"
[root@oracle tmp]# myname0=‘My name is $name‘
[root@oracle tmp]# myname1="My name is ‘$name‘"
[root@oracle tmp]# myname2=‘My name is "$name"‘
[root@oracle tmp]# myname3="My name is "$name""
[root@oracle tmp]# myname4=‘My name is ‘$name‘‘
[root@oracle tmp]# echo $myname0
My name is $name
[root@oracle tmp]# echo $myname1
My name is ‘hello‘
[root@oracle tmp]# echo $myname2
My name is "$name"
[root@oracle tmp]# echo $myname3
My name is hello
[root@oracle tmp]# echo $myname4
My name is hello
[root@oracle tmp]#
符号 | 意义 |
\n | 新的一行 |
\t | 表示tab键 |
[root@oracle ~]# echo -e "\tabc123"
abc123
[root@oracle ~]# echo -e "\tabc\n123"
abc
123
[root@oracle ~]#
[root@oracle tmp]# cat read.sh
#!/bin/bash
##############################
#Function: read using
#Us
#Author: wangyu
#Company: haitianjingyu
#Version: 1.0
##############################
read -p "please input you name:" name
read -n3 -s -p "input password:" pwd
echo -e "\n you username is $name ,password is $pwd"
[root@oracle tmp]# bash read.sh
please input you name:hai
input password:
you username is hai ,password is hai
[root@oracle tmp]#
#!/bin/bash
##############################
#Function: menu
#Author: wangyu
#Company: haitianjingyu
#Version: 1.0
##############################
clear
echo -e "\n\t\t\t***system menager tool***"
echo -e "\n\t1.\tshow disk space info"
echo -e "\t2.\tshow network connector info"
echo -e "\t3.\tshow memory using info"
echo -e "\t0.\texit menu"
echo -en "\n\t\tinput option:"
read -n1 num
echo -e "\nyou option is $num"
[root@oracle tmp]# cat adduser.sh
#!/bin/bash
##########################
#Function: create new user
#Author: wangyu
#Company: haitianjingyu
#Version: 1.0
##########################
read -p "add new user:" name
read -s -p "user $name password:" pwd
useradd $name
echo $pass|passwd --stdin $name &>/dev/null
echo -e "\nuser $name is ok"
[root@oracle tmp]# bash adduser.sh
add new user:hai2
user hai2 password:
user hai2 is ok
[root@oracle tmp]#
#!/bin/bash
read -p "请输入查找的文件夹: " DIR
for i in "$@"
do
if(i!=0){
NUM=$(find $DIR -type f -name "*$i" | wc -l)
echo "以$i结尾的文件的数目是: " $NUM
}
fi
done
read -p "请输入一个挂载点的目录绝对路径:" DIR
mkdir -p $DIR
umount /dev/sr0 &> /dev/null
mount /dev/sr0 $DIR &> /dev/null
(cd /etc/yum.repos.d; tar *.repo *repo.tar &> /dev/null)
cat << EOF > /etc/yum.repos.d/yumlocal.repo
[Server]
name=Server
baseurl=file:///$DIR
enabled=1
gpgcheck=0
EOF
yum clean all &> /dev/null && echo yum is ok标签:etc 根据 英文字母 条件 编程 引号 选项 后缀 bash
原文地址:http://www.cnblogs.com/haitianjingyu/p/6973709.html