shell-01

Chapter 1 如何才能学好shell编程
	1.0多写、多写;每个代码都要写

Chapter 2 Shell脚本
	2.1 什么是shell?
	2.2 什么是shell脚本?
	2.3 shell脚本的地位
	2.4 shell脚本的种类 B shell(bash、sh)、C shell
	2.5 常用的操作系统默认的shell
		[[email protected] ~]# cat /etc/shells
		/bin/sh
		/bin/bash
		/sbin/nologin
		/usr/bin/sh
		/usr/bin/bash
		/usr/sbin/nologin
		[[email protected] ~]# grep root /etc/passwd
		root:x:0:0:root:/root:/bin/bash
	2.6 shell脚本的执行
		执行顺序:查找ENV(/etc/profile .bash_profile .bashrc /etc/bashrc)加载环境变量;执行shell脚本内容
		执行方式:sh ***.sh  ./***.sh  cat ***.sh | sh
		基本规范:写解释器 写info 写注释
		书写习惯:成对符号要一次性写出 中括号两端要留有空格 流程控制语句要写完 代码要缩进
		引号使用:常规变量要双引号 a="/tmp/srm"
							强引用(所见即所得)的要用单引号 echo ‘qwejklsdmkasj\asdj‘
							命令引用要用反引号`whoami`

Chapter 3 shell变量
	3.1什么是变量 不区分类型 分局部变量和全局变量
	3.2环境变量
		显示变量的值:set(所有变量) env(全局变量) declare(变量、函数等)
		自定义环境变量:export a="123"或者declare -x a="1234"
			[[email protected] ~]# export a="123"
			[[email protected] ~]# echo $a
			123
			[[email protected] ~]# declare -x a="1234"
			[[email protected] ~]# echo $a
			1234
		取消环境变量:unset
		如何永久生效:写入环境变量
			1.用户环境变量配置文件 /root/.bashrc /root/.bash_profile
			2.全局环境配置变量/etc/profile,/etc/bashrc,/etc/profile.d/
		显示环境变量?echo $a
	3.3普通变量
		3.3.1定义本地变量
			将命令的结果作为变量的内容进行赋值··(反引号)或者$()
			字符串拼接:直接拼接即可。tar -zcvf `uname -n`_$DATE.tar.gz /Storage/Plus/webapp/storage
		3.3.2和awk、sed、grep配合使用
			对sed、grep支持较好;awk不太好

Chapter 4 shell变量知识进阶与实践
	4.1特殊且重要的变量
	$0 shell脚本的执行路径+
	$n 第n个参数,如果大于9,需要用{}引用,如${10}
	$# 获取脚本传参的总个数
	$* 获取后边的参数,加双引号代表“$1 $2 $3”
	[email protected] 获取后边的参数,加双引号代表“$1” "$2" “$3”

	$? 上一个状态的返回值
	$$ 当前shell脚本的PID
	$! 上一个在后台进程的PID
	$_ 获取上一个命令或脚本的最后一个参数

	[[email protected] ~]# sh /root/rpcbind.sh  start  12ew  123
	start
	/root/rpcbind.sh      $0
	3										  $#
	start 12ew 123       	$*
	start 12ew 123        [email protected]

	4.2内置命令
	echo
	eval 当shell执行到eval语句时候,shell读入参数args,并将它们组合为一个新的命令并执行
	exec 在当前shell执行命令,执行完毕后该shell终止
	read 读取用户输入

	4.3shell变量字符串知识
	${parameter} 字符串内容
	${#parameter} 字符串长度 wc -L / awk带的length函数、expr带的length函数
	ARRAY=(
	i
	am
	oldboy
	linux
	,
	welcome
	to
	our
	training
	classroom
	)
	##shell数组的方法
	for ((i=0;i<${#array[*]};i++));
	 do
	 	if [ ${#array[$i]} -lt 6 ]; then
	 		echo "${array[$i]}"
	 	fi
	done

	echo "-----------------------------------"
	##遍历数组的两种方法
	for word in ${array[*]}
		do
			if [ `echo $word | wc -L` -lt 6 ];then
				echo $word
			fi
		done
	##for循环实现和第二种遍历数组方法差不多?!
	for i in i am oldboy linux,welcome to our training classroom; do
		if [[ `echo $i|wc -L ` -lt 6 ]]; then
			echo $i
		fi
	done
	##awk的方法 也是写for循环进行遍历,和C的方法一样
	echo $ARRAY | awk ‘BEGIN{}{for(i=1;i<=NF;i++) if(length($i)<6) print $i}END{}‘

 

 

时间: 2024-07-30 11:31:22

shell-01的相关文章

Linux Shell 01 脚本与变量

一 脚本的创建和执行 1. 创建文件*.sh,文件后缀为sh 2. 编辑脚本 首行必须为:#!/bin/bash   #指定解释脚本的shell 3. 赋权 chmod u+x a.sh 4. 执行 ./a.sh 二 shell脚本中的变量 1.变量的定义与删除 name="hello" #定义变量=前后不能有空格 unset name #删除变量 a. 当前shell中定义的变量只在当前shell有效 b. 不论是普通变量还是环境变量,归根结底都是变量,都遵循变量的基本操作 2.环境

Shell.01.shell脚本的格式和执行

1.标明shell类型: #!/bin/bash 或 #!/bin/python, etc. #!/bin/bash #Author: X #Date: 2020-02-07 #Version: 1.0 #Description: For Shell study 2.更改脚本为可执行: chmod +x yourShell.sh 3.执行 方式1: ./yourShell.sh 方式2: bash yourShell.sh 4.远程调用脚本在本机执行 curl http://ip/yourShe

linux shell 01

.sh  文件执行权限赋予    chmod ( -u  root  )-x  script.sh 单引号 无视变量   双引号 执行变量 例: a1='phone' a2='this is a ${a1}' echo ${a2} 结果:  this is a ${a1} a1='phone' a2="this is a ${a1}" echo ${a2} 结果:  this is a phone 原文地址:https://www.cnblogs.com/Lorentz-Z/p/864

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量

- 20.1 shell脚本介绍 - 20.2 shell脚本结构和执行 - 20.3 date命令用法 - 20.4 shell脚本中的变量 # 20.1 Shell脚本介绍 -  shell是一种脚本语言  关注aming_linux  blog.lishiming.net -  可以使用逻辑判断.循环等语法 -  可以自定义函数 -  shell是系统命令的集合 -  shell脚本可以实现自动化运维,能大大增加我们的运维效率 # 20.2 Shell脚本结构和执行 - 开头需要加#!/b

20.1-20.4 shell:脚本,脚本结构与执行,date用法,shell脚本的变量

20.1 shell是什么 shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具. 实际上,他在shell和计算机硬件之间还有一层东西--系统硬核.如果把计算机硬件比作一个人的躯体,那系统内核就是人的电脑.至于shell,把它比作人的五官似乎更贴切些. 用户直接面对的不是计算机硬件而是shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作. shell是一种脚本语言  微信公众号:aming_linux  博客:blog.

shell脚本介绍 shell脚本结构和执行 date命令用法 shell脚本中的变量

一.shell脚本介绍shell脚本要想写好,必须通过不断地去练习写才能写好,没有捷径要在我们拿到一个需求的时候有一个脚本的大致思路,想到需求怎么去实现shell脚本可以大大提高我们的工作效率二.shell脚本结构和执行[[email protected] ~]# mkdir shell //创建一个shell文件夹,存放实验的shell脚本[[email protected] ~]# cd shell/[[email protected] shell]# ls[[email protected

git ,创建生成 making git-svn work on mac tiger

http://www.mikeheijmans.com/2008/04/make-git-svn-work-on-mac-osx-tiger/ After a few hours of googling and pull some hair out, I have finally figured out how to make git-svn work on Mac OSX 10.4 Tiger. If you have installed git on your Mac using mac-p

2018-4-27 18周2次课 分发系统-expect讲解(下)

20.31 expect脚本同步文件 ·自动同步文件 [[email protected] sbin]# chmod a+x 4.expect [[email protected] sbin]# ./4.expect spawn rsync -av [email protected]:/tmp/12.txt /tmp/ [email protected]'s password: receiving incremental file list 12.txt sent 30 bytes  recei

我喜欢减肥我们来减肥吧

http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313278016/2015.01.28.html http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313282016/2015.01.28.html http://www.ebay.com/cln/honus.jyw4mvptb/cars/158313289016/2015.01.28.html http://www.ebay.com/cln/usli

百度回家看沙发沙发是减肥了卡斯加积分卡拉是减肥

http://www.ebay.com/cln/hpryu-caw8ke/cars/158056866019/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/158445650015/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/158445674015/2015.01.31 http://www.ebay.com/cln/xub.50x2l7cj/cars/1584456790