码迷,mamicode.com
首页 > 系统相关 > 详细

shell学习之变量

时间:2015-05-20 20:44:37      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:shell   变量

一
 变量定义
	1 显式定义 variable=XXX   没有空格
	2 read variable读取
		主要写了如下两行代码,读取name和name
family
		read -p "please input your name:" 
name
		read -p "please input your whole name :"
name family
		运行下
		[root@www shell]# ./var.sh
		please input your name:liuliancao
		your name is :liuliancao
		please input your name:liu liancao
		your name is :liu liancao
		please input your whole name :liancao liu
		your name is liancao,your family is liu
		please input your whole name :liancao
		your name is liancao,your family is
		please input your whole name :liancao l iu
		your name is liancao,your family is l iu
		可见
变量个数和所输入的个数一样,则分配正好,如果变量个数多了,则后面的变量为空,变量个数少了,那么最后面的是老大(吃最多)。
	3 从文件中读取
		[root@www shell]# read message < txt
		[root@www shell]# echo $message
		hello,i am
liuliancao由于是文字重定向,且txt文本行间有换行符,所以此时只有一行,如果想持续就得使用循环
		[root@www shell]# while read message
		> do
		> echo $message
		> done < txt
		hello,i am liuliancao
		this is a test
	4 命令替换
		
      1 #!/bin/bash
      2 #
	  time=`date +%c`
      3 echo "now time is $time"
	  执行结果
		[root@www shell]# ./com.sh
		now time is Sat 09 May 2015 12:26:49 PM CST	
	5 位置参数
		常见的有
		$0 完整文件名 basename可以取基本名字
		$1 第一个变量内容,依次类推
		$@ 所有变量
以数组方式存 $* 所有变量,归为大字符串
	6 删除变量
		unset 变量名称


shell学习之变量

标签:shell   变量

原文地址:http://qixue.blog.51cto.com/7213178/1653289

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!