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

  1. #!/bin/bash
  2. if [ "${1##*.}" = "tar" ]
  3. then
  4.    echo This appears to be a tarball.
  5. else
  6.    echo At first glance, this does not appear to be a tarball.
  7. 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


  1. #!/usr/bin/env bash
  2.   echo name of script is $0
  3.   echo first argument is $1
  4.   echo second argument is ${2}
  5.   echo seventeenth argument is ${17}
  6.   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

  1. #!/usr/bin/env bash
  2. #allargs.sh
  3. for thing in "$@"
  4. do
  5.   echo you typed ${thing}.
  6. done

$ allargs.sh hello there you silly

you typed hello.
you typed there.
you typed you.
you typed silly.

  1. if [[ "$myvar" -gt 3 ]]
  2. then
  3.    echo "myvar greater than 3"
  4. fi
  1. if [[ "$myvar" == "3" ]]
  2. then
  3.   echo "myvar equal 3"
  4. 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.

  1. if [ $myvar = "foo bar oni" ]
  2. then
  3.    echo "yes"
  4. 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:

  1. if [ "$myvar" == "foo bar oni" ]
  2. then
  3.   echo "yes"
  4. fi

OR:

  1. if [[ $myvar == "foo bar oni" ]]
  2. then
  3.   echo "yes"
  4. fi

The best method:

  1. if [[ "$myvar" == "foo bar oni" ]]
  2. then
  3.   echo "yes"
  4. 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

  1. #!/bin/env bash
  2. myvar=0
  3. while [[ "$myvar" -ne 10 ]]
  4.   do
  5.   echo $myvar
  6.   myvar=$(( $myvar + 1 ))
  7. done
  1. #!/bin/env bash
  2. myvar=0
  3. until [[ $myvar -eq 10 ]]
  4. do
  5.   echo $myvar
  6.   myvar=$(( $myvar + 1 ))
  7. done
  1. tarview() {
  2.   echo "Displaying contents of $@"
  3.   for x in $@
  4.     do
  5.     if [[ "${x##*.}" == "tar" ]]
  6.     then
  7.       echo "(uncompressed tar $x)"
  8.       cat $x | tar -tvf -
  9.     elif [[ "${x##*.}" == "gz" ]]
  10.     then
  11.       echo "(gzip-compressed tar $x)"
  12.       tar ztvf $x
  13.     elif [[ "${x##*.}" == "bz2" ]]
  14.     then
  15.       echo "(bzip2-compressed tar $x)"
  16.       cat $x | bunzip2 - | tar -tvf -
  17.     fi
  18.   done
  19. }
  1. myvar="hello"
  2. myfunc() {
  3.   myvar="one two three"
  4.   for x in $myvar
  5.   do
  6.     echo $x > /dev/null
  7.   done
  8. }
  9. myfunc
  10. echo $myvar $x
  1. myvar="hello"
  2. myfunc() {
  3.   local x  
  4.   local myvar="one two three"
  5.   for x in $myvar
  6.   do
  7.     echo $x > /dev/null
  8.   done
  9. }
  10. myfunc
  11. echo $myvar $x

『BASH』——Learn BashScript from Daniel Robbins——[001-002]的更多相关文章

  1. 『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 ...

  2. 『BASH』——文件权限批量恢复脚本——「Permission Revovery」

    一.恢复指定程序包所有文件的权限: #!/bin/bash #Assume that you have mounted a correct orignal-system on /mnt read -p ...

  3. 『BASH』——Hadex's brief analysis of "Lookahead and Lookbehind Zero-Length Assertions"

    /*为节省时间,本文以汉文撰写*/ -前言- 深入学习正则表达式,可以很好的提高思维逻辑的缜密性:又因正则应用于几乎所有高级编程语言,其重要性不言而喻,是江湖人士必备的内功心法. 正则表达式概要(ob ...

  4. 『AngularJS』$location 服务

    项目中关于 $location的用法 简介 $location服务解析在浏览器地址栏中的URL(基于window.location)并且让URL在你的应用中可用.改变在地址栏中的URL会作用到$loc ...

  5. [原创] 【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 ...

  6. JS 中通过对象关联实现『继承』

    JS 中继承其实是种委托,而不是传统面向对象中的复制父类到子类,只是通过原型链将要做的事委托给父类. 下面介绍通过对象关联来实现『继承』的方法: Foo = { // 需要提供一个 init 方法来初 ...

  7. 『摄影欣赏』16幅 Romantic 风格照片欣赏【组图】

    今天,我们将继续分享人类情感的系列文章.爱是人类最重要的感觉,也可能是各种形式的艺术(电影,音乐,书,画等)最常表达的主题 .这里有40个最美丽的爱的照片,将激励和给你一个全新的视觉角度为这种情绪.我 ...

  8. 『开源』Slithice 2013 服务器集群 设计和源码

    相关介绍文章: <『设计』Slithice 分布式架构设计-支持一体式开发,分布式发布> <『集群』001 Slithice 服务器集群 概述> <『集群』002 Sli ...

  9. 『片段』OracleHelper (支持 多条SQL语句)

    C# 调用 Oracle 是如此尴尬 >System.Data.OracleClient.dll —— .Net 自带的 已经 过时作废. >要链接 Oracle 服务器,必须在 本机安装 ...

随机推荐

  1. 数据结构和算法设计专题之---二分查找(Java版)

    1.前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序 2.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中值,中值后:将要查找的值和数组的中值 ...

  2. input限制字符长度 - composition

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. f-li.cn

    package org.rx.service.command.impl; import lombok.Getter; import lombok.Setter; import org.rx.core. ...

  4. 带头结点的循环单链表----------C语言

    /***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...

  5. samba环境部署

    安装samba yum install -y samba samba-client vim /etc/samba/smb.conf (共享一个目录,任何人都可以访问但不能写修改) workgroup ...

  6. 拾遗:Go 代码结构

    #define @ $ 概述 Go 编码通常将所有代码放置在同一个工作区 一个工作区包含多个版本仓库,使用 Git 等工具控制 每个仓库包含一个或多个包 每个包由同一目录中的一份或多份源代码组成 包的 ...

  7. 使用ProxyBroker构建代理池

    import asyncio from proxybroker import Broker async def show(proxies): while True: proxy = await pro ...

  8. 4-vim-工作模式-01-职责以及切换模式

    vi 有三种工作模式 1.命令模式 打开文件首先进入命令模式,是使用vi的入口. 通过命令对文件进行常规的编辑操作,例如:定位-翻页-复制-粘贴-删除等. 在其他图形编辑器下,通过快捷键或鼠标实现的操 ...

  9. nashorn中js数组转为对象的问题

    背景 在项目中,使用jdk中的nashorn执行javascript脚本,例如如下脚本片段: let ctx = session.ctx; ctx.confirm = { //车牌划分后的数组 seg ...

  10. mongo之$rename更新一个字段的名称(可批量)

    $rename操作逻辑执行$unset 老名称和新的名称,然后执行 $set新名称运行.因此,操作可能不保留文档中字段的顺序; 即重命名的字段可以在文档内移动. 1.重命名字段:一级字段 格式: db ...