1、Give ECHO colors see see!

NORMAL=$(tput sgr0)
GREEN=$(tput setaf ; tput bold)
YELLOW=$(tput setaf )
RED=$(tput setaf ) function red() {
echo -e "$RED$*$NORMAL"
} function green() {
echo -e "$GREEN$*$NORMAL"
} function yellow() {
echo -e "$YELLOW$*$NORMAL"
} # To print success
green "Task has been completed" # To print error
red "The configuration file does not exist" # To print warning
yellow "You have to use higher version."

关于上面这个,我个人感觉不是很好,还有别的方法,以后再说吧!

2、打印 Debug 信息

function debug() {
if [[ $DEBUG ]]
then
echo ">>> $*"
fi
} # For any debug message
debug "Trying to find config file"

合并成一行:

# From cool geeks at hacker news
function debug() { ((DEBUG)) && echo ">>> $*"; }
function debug() { [ "$DEBUG" ] && echo ">>> $*"; }

3、检查可执行文件是否存在:

OK=
FAIL= function require_curl() {
which curl &>/dev/null
if [ $? -eq ]
then
return $OK
fi return $FAIL
}

其实也就一行就可以解决:function require_curl() {which culr &>/dev/null;}

4、打印命令的使用方式:

cat << EOF

Usage: myscript <command> <arguments>

VERSION: 1.0

Available Commands

    install - Install package

    uninstall - Uninstall package

    update - Update package

    list - List packages

EOF

5、给变量加默认值:

URL=${URL:-http://localhost:8080}

6、检查字符串变量的长度:

if [ ${#authy_api_key} !=  ]
then
red "you have entered a wrong API key"
return $FAIL
fi

7、半延迟读取数据:

READ_TIMEOUT=
read -t "$READ_TIMEOUT" input # if you do not want quotes, then escape it
input=$(sed "s/[;\`\"\$\' ]//g" <<< $input) # For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*//g' <<< $input)

8、获取目录和文件的名称:

# To find base directory
APP_ROOT=`dirname "$0"` # To find the file name
filename=`basename "$filepath"` # To find the file name without extension
filename=`basename "$filepath" .html`

此文出自老外,觉得挺好,就译了过来!不算盗版吧,哈哈!

学一点,收获一点。学到的都是自己的!

shell 脚本技巧的更多相关文章

  1. shell脚本技巧记录

    2014/4/9 shell脚本变量处理: ${varible##*string} //从左向右截取最后一个string后的字符串 ${varible#*string} //从左向右截取第一个stri ...

  2. shell 脚本实战笔记(10)--spark集群脚本片段念念碎

    前言: 通过对spark集群脚本的研读, 对一些重要的shell脚本技巧, 做下笔记. *). 取当前脚本的目录 sbin=`dirname "$0"` sbin=`cd &quo ...

  3. shell脚本小实例

    本文收集了一堆的shell脚本技巧,我说过,我写博客主要是作一些学习笔记,方便自己查阅,所以,我会搞出这么一篇文章,也没有什么不可理解的.关于这些技巧的出处,诶,我也忘了,可能来自theunixsch ...

  4. 写好shell脚本的13个技巧【转】

    有多少次,你运行./script.sh,然后输出一些东西,但却不知道它刚刚都做了些什么.这是一种很糟糕的脚本用户体验.我将在这篇文章中介绍如何写出具有良好开发者体验的 shell 脚本. 产品的最终用 ...

  5. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

  6. shell脚本常用技巧

    shell脚本常用技巧 1.获取随机字符串或数字 ~]#echo $RANDOM | md5sum | cut -c 1-6 ~]#openssl rand -base64 4 | cut -c 1- ...

  7. Docker学习3-简单shell脚本安装mysql5.7与docker小技巧

    前言 玩过Windows中的 .bat 的小伙伴是不是觉得很有意思呢,github中一键推送.同步拉取等等操作,哈哈,当然shell脚本也是很类似,可以运行一个脚本就可以自动给我们部署好环境啦!但是这 ...

  8. 6条shell小技巧,让脚本显得不再业余【转】

    如何能让自己的shell显得不那么业余?下面6点实践一定有用. 画外音:本篇文章源自Google的一篇实践,抽取了部分1分钟能读完的内容,加入了一些分析. 一.以下面的语句开场 set -o noun ...

  9. Shell脚本字符串匹配及日常命令工具 - 用法总结(技巧指南)

    Shell提供了很多字符串和文件处理的命令,如awk.expr.grep.sed等命令,还有文件的排序.合并和分割等一系列的操作命令.下面重点总结下Shell字符串处理.文本处理以及各类命令及函数用法 ...

随机推荐

  1. UI数据库

    一.数据库 SQL: SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集, 是一种功能齐全的数据库语言. 二.数据库管理系统 数据 ...

  2. centos 6 安装 gitlib

    安装gitlab-----------1. 下载 gitlabcurl -O https://downloads-packages.s3.amazonaws.com/centos-6.5/gitlab ...

  3. ARMLinux下Alignment trap的一些测试 【转自 李迟的专栏 CSDN http://blog.csdn.net/subfate/article/details/7847356

    项目中有时会遇到字节对齐的问题,英文为“Alignment trap”,如果直译,意思为“对齐陷阱”,不过这个说法不太好理解,还是直接用英文来表达. ARM平台下一般是4字节对齐,可以参考文后的给出的 ...

  4. matlab初学之textread

    文章出处:http://blog.sina.com.cn/s/blog_9e67285801010bju.html 基本语法是: [A,B,C,-] = textread(filename,forma ...

  5. [Python]记录详细调用堆栈的日志

    参考http://www.cnblogs.com/tuzkee/p/3243110.html import sys import os def detailtrace(info): retStr = ...

  6. Oracle 数据库--一个用户同步的sql

    用户同步的sql: insert into crm_customer_user ,username,,,,,id, from sys_user where username not in (selec ...

  7. Typescript中的class interface 只是在声明,其实什么也没有干!

    由于vue.js的特殊机制,初始化后给对象添加的属性是无法更新UI的. 最近结合typescript和vue进行web开发,就遇到了这样的坑. class user{ name:string; age ...

  8. ACM_1001_Exponentiation 详解

    参考:http://blog.csdn.net/rually/article/details/8585268 #include<iostream> using namespace std; ...

  9. Android四大组件-Activity

    http://android.tgbus.com/Android/androidnews/201204/419485.shtml

  10. 一个android参考网站,工具+源码

    Android多渠道打包工具  https://github.com/wubo/apptools Android官方培训课程中文版(v0.9.4)  http://hukai.me/android-t ...