『BASH』——Learn BashScript from Daniel Robbins——[001-002]
ABSTRACT:
Daniel Robbins is best known as the creator of Gentoo Linux and author of many IBM developerWorks articles about Linux. Daniel currently serves as Benevolent Dictator for Life (BDFL) of Funtoo Linux. Funtoo Linux is a Gentoo-based distribution and continuation of Daniel's original Gentoo vision.
Section 1
$ myvar='This is my environment variable!'
$ echo $myvar
This is my environment variable!
$ echo foo$myvarbar
foo
$ echo foo${myvar}bar
fooThis is my environment variable!bar
$ echo foo"${myvar}"bar
fooThis is my environment variable!bar
$ echo foo"$myvar"bar
fooThis is my environment variable!bar
$ basename /usr/local/share/doc/foo/foo.txt
foo.txt
$ basename /usr/home/drobbins
drobbins
$ dirname /usr/local/share/doc/foo/foo.txt
/usr/local/share/doc/foo
$ dirname /usr/home/drobbins/
/usr/home
$ MYFILES=$(ls /etc | grep pa)
$ echo $MYFILES
pam.d passwd
$ MYFILES=$(ls $(dirname foo/bar/oni))
NOTE:$( ) is generally preferred over ` ` in shell scripts,because it is more universally supported across different shells,it is less complicated to use in a nested form.
$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg
$ MYFOO="chickensoup.tar.gz"
$ echo ${MYFOO%%.*}
chickensoup
$ echo ${MYFOO%.*}
chickensoup.tar
$ MYFOOD="chickensoup"
$ echo ${MYFOOD%%soup}
chicken
- $ MYFOOD="soupchickensoup"
- $ echo ${MYFOOD%soup}
- soupchicken
- $ echo ${MYFOOD%%soup}
- soupchicken
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:5}
abung
- $ echo ${EXCLAIM:3}
- abunga
NOTE:standard format is ${VAR:offset:length},if there is no ":length" given,then default to the end of the original variable
$ MYFOOD="chickensoup and another dogsoup"
$ echo ${MYFOOD/soup/rubbish}
chickenrubbish and another dogsoup
$ echo ${MYFOOD//soup/rubbish}
chickenrubbish and another dogrubbish
- #!/bin/bash
- if [ "${1##*.}" = "tar" ]
- then
- echo This appears to be a tarball.
- else
- echo At first glance, this does not appear to be a tarball.
- fi
$ ./mytar.sh thisfile.tar
This appears to be a tarball.
$ ./mytar.sh thatfile.gz
At first glance, this does not appear to be a tarball.
Section 2
- #!/usr/bin/env bash
- echo name of script is $0
- echo first argument is $1
- echo second argument is ${2}
- echo seventeenth argument is ${17}
- echo number of arguments is $#
NOTE:bash features the "$@" variable, which expands to all command-line parameters separated by spaces.
NOTE:“$*”,not be separated,as one new parameter
- #!/usr/bin/env bash
- #allargs.sh
- for thing in "$@"
- do
- echo you typed ${thing}.
- done
$ allargs.sh hello there you silly
you typed hello.
you typed there.
you typed you.
you typed silly.
- if [[ "$myvar" -gt 3 ]]
- then
- echo "myvar greater than 3"
- fi
- if [[ "$myvar" == "3" ]]
- then
- echo "myvar equal 3"
- fi
NOTE:In the above two comparisons do exactly the same thing, but the first uses arithmetic comparison operators, while the second uses string comparison operators.
NOTE:the second can't be used to [[ "$myvar" > "10" ]],because "2" is larger than "10" in the comparision of string.
- if [ $myvar = "foo bar oni" ]
- then
- echo "yes"
- fi output [: too many arguments
NOTE:In this case, the spaces in "$myvar" (which equals "foo bar oni") end up confusing bash. After bash expands "$myvar", it ends up with the following comparison:[ foo bar oni = "foo bar oni" ].
Because the environment variable wasn't placed inside double quotes, bash thinks that you stuffed too many arguments in-between the square brackets. You can easily eliminate this problem by surrounding the string arguments with double-quotes or use double square brackets. Remember, if you get into the habit of surrounding all string arguments and environment variables with double-quotes, you'll eliminate many similar programming errors. Here's how the "foo bar oni" comparison should have been written:
- if [ "$myvar" == "foo bar oni" ]
- then
- echo "yes"
- fi
OR:
- if [[ $myvar == "foo bar oni" ]]
- then
- echo "yes"
- fi
The best method:
- if [[ "$myvar" == "foo bar oni" ]]
- then
- echo "yes"
- fi SO!!! use [[ ]] instead of [ ]
$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo
$(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57
- #!/bin/env bash
- myvar=0
- while [[ "$myvar" -ne 10 ]]
- do
- echo $myvar
- myvar=$(( $myvar + 1 ))
- done
- #!/bin/env bash
- myvar=0
- until [[ $myvar -eq 10 ]]
- do
- echo $myvar
- myvar=$(( $myvar + 1 ))
- done
- tarview() {
- echo "Displaying contents of $@"
- for x in $@
- do
- if [[ "${x##*.}" == "tar" ]]
- then
- echo "(uncompressed tar $x)"
- cat $x | tar -tvf -
- elif [[ "${x##*.}" == "gz" ]]
- then
- echo "(gzip-compressed tar $x)"
- tar ztvf $x
- elif [[ "${x##*.}" == "bz2" ]]
- then
- echo "(bzip2-compressed tar $x)"
- cat $x | bunzip2 - | tar -tvf -
- fi
- done
- }
- myvar="hello"
- myfunc() {
- myvar="one two three"
- for x in $myvar
- do
- echo $x > /dev/null
- done
- }
- myfunc
- echo $myvar $x
- myvar="hello"
- myfunc() {
- local x
- local myvar="one two three"
- for x in $myvar
- do
- echo $x > /dev/null
- done
- }
- myfunc
- echo $myvar $x
『BASH』——Learn BashScript from Daniel Robbins——[001-002]的更多相关文章
- 『BASH』——Learn BashScript from Daniel Robbins——[003]
ABSTRACT: Daniel Robbins is best known as the creator of Gentoo Linux and author of many IBM develop ...
- 『BASH』——文件权限批量恢复脚本——「Permission Revovery」
一.恢复指定程序包所有文件的权限: #!/bin/bash #Assume that you have mounted a correct orignal-system on /mnt read -p ...
- 『BASH』——Hadex's brief analysis of "Lookahead and Lookbehind Zero-Length Assertions"
/*为节省时间,本文以汉文撰写*/ -前言- 深入学习正则表达式,可以很好的提高思维逻辑的缜密性:又因正则应用于几乎所有高级编程语言,其重要性不言而喻,是江湖人士必备的内功心法. 正则表达式概要(ob ...
- 『AngularJS』$location 服务
项目中关于 $location的用法 简介 $location服务解析在浏览器地址栏中的URL(基于window.location)并且让URL在你的应用中可用.改变在地址栏中的URL会作用到$loc ...
- [原创] 【2014.12.02更新网盘链接】基于EasySysprep4.1的 Windows 7 x86/x64 『视频』封装
[原创] [2014.12.02更新网盘链接]基于EasySysprep4.1的 Windows 7 x86/x64 『视频』封装 joinlidong 发表于 2014-11-29 14:25:50 ...
- JS 中通过对象关联实现『继承』
JS 中继承其实是种委托,而不是传统面向对象中的复制父类到子类,只是通过原型链将要做的事委托给父类. 下面介绍通过对象关联来实现『继承』的方法: Foo = { // 需要提供一个 init 方法来初 ...
- 『摄影欣赏』16幅 Romantic 风格照片欣赏【组图】
今天,我们将继续分享人类情感的系列文章.爱是人类最重要的感觉,也可能是各种形式的艺术(电影,音乐,书,画等)最常表达的主题 .这里有40个最美丽的爱的照片,将激励和给你一个全新的视觉角度为这种情绪.我 ...
- 『开源』Slithice 2013 服务器集群 设计和源码
相关介绍文章: <『设计』Slithice 分布式架构设计-支持一体式开发,分布式发布> <『集群』001 Slithice 服务器集群 概述> <『集群』002 Sli ...
- 『片段』OracleHelper (支持 多条SQL语句)
C# 调用 Oracle 是如此尴尬 >System.Data.OracleClient.dll —— .Net 自带的 已经 过时作废. >要链接 Oracle 服务器,必须在 本机安装 ...
随机推荐
- 经典排序背包——cf1203F
先把收益为正数的处理掉:策略是挨个扫,扫n遍,碰到能买的就买,然后可以得到一个更新后的r 剩下的就看做是一个背包模型:物品(a,b)表示当背包体积>a时才能装下体积为b的该物品,问最多装几个 无 ...
- Github代码管理教程
https://desktop.github.com/ 目录 Create and use a repository Start and manage a new branch Make change ...
- AcWing 143. 最大异或对 01字典树打卡
在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...
- NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用
关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...
- systemctl命令配置系统服务
1.systemd的配置文件目录 systemd将daemon执行的脚本视作服务单位(unit),服务依据功能区分时,分为不同的类型(type). 常见的systemd服务类型如下表: 后缀名称 ...
- System之nanoTime函数
原文地址:https://blog.csdn.net/yumolan4325/article/details/79201766 1 System有一个静态的函数nanoTime函数,该函数是返回纳秒的 ...
- MDK中问题:warning : type qualifier is meaningless on cast type return 的解决
在MDK编译代码时,有时会出现这样的警告, warning : type qualifier is meaningless on cast type return 在MDK中,作如下设置: 即添加 : ...
- Linux的命名空间
1. 为什么提供命名空间 命名空间是一种轻量级的虚拟化手段. 传统的虚拟化软件,是虚拟化多个不同的操作系统,对共享资源的限制很大. 通过提供命名空间,可以让进程与进程之间,用户与用户之间彼此看不到对方 ...
- Ubuntu12.04下删除文件夹内所有的.svn文件
前段时间在公司里遇到一个问题,从svn上下载下来的文件含有.svn文件,如何删除当前目录下的所有.svn文件呢? 一个个手动删除显然不可能,太多了.其实在Ubuntu12.04下延伸至其他所搜的Lin ...
- linux下读取移动硬盘
前提是安装了ntfs-3g软件,系统才能识别到移动硬盘. 第一步.fdisk -l 该命令查看系统识别到的磁盘,如果移动硬盘系统能够识别, 在屏幕上会输出“/dev/sdb1”之类的字样. ...