shell 脚本技巧
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 脚本技巧的更多相关文章
- shell脚本技巧记录
2014/4/9 shell脚本变量处理: ${varible##*string} //从左向右截取最后一个string后的字符串 ${varible#*string} //从左向右截取第一个stri ...
- shell 脚本实战笔记(10)--spark集群脚本片段念念碎
前言: 通过对spark集群脚本的研读, 对一些重要的shell脚本技巧, 做下笔记. *). 取当前脚本的目录 sbin=`dirname "$0"` sbin=`cd &quo ...
- shell脚本小实例
本文收集了一堆的shell脚本技巧,我说过,我写博客主要是作一些学习笔记,方便自己查阅,所以,我会搞出这么一篇文章,也没有什么不可理解的.关于这些技巧的出处,诶,我也忘了,可能来自theunixsch ...
- 写好shell脚本的13个技巧【转】
有多少次,你运行./script.sh,然后输出一些东西,但却不知道它刚刚都做了些什么.这是一种很糟糕的脚本用户体验.我将在这篇文章中介绍如何写出具有良好开发者体验的 shell 脚本. 产品的最终用 ...
- SHELL脚本编程的常识和VI常用技巧
来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...
- shell脚本常用技巧
shell脚本常用技巧 1.获取随机字符串或数字 ~]#echo $RANDOM | md5sum | cut -c 1-6 ~]#openssl rand -base64 4 | cut -c 1- ...
- Docker学习3-简单shell脚本安装mysql5.7与docker小技巧
前言 玩过Windows中的 .bat 的小伙伴是不是觉得很有意思呢,github中一键推送.同步拉取等等操作,哈哈,当然shell脚本也是很类似,可以运行一个脚本就可以自动给我们部署好环境啦!但是这 ...
- 6条shell小技巧,让脚本显得不再业余【转】
如何能让自己的shell显得不那么业余?下面6点实践一定有用. 画外音:本篇文章源自Google的一篇实践,抽取了部分1分钟能读完的内容,加入了一些分析. 一.以下面的语句开场 set -o noun ...
- Shell脚本字符串匹配及日常命令工具 - 用法总结(技巧指南)
Shell提供了很多字符串和文件处理的命令,如awk.expr.grep.sed等命令,还有文件的排序.合并和分割等一系列的操作命令.下面重点总结下Shell字符串处理.文本处理以及各类命令及函数用法 ...
随机推荐
- c/c++面试题(8)memcopy/memmove/atoi/itoa
1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...
- ldap + kerberos 整合
第一部分:ldap1. 安装ldap yum install -y openldap openldap-clients openldap-servers openldap-devel 2. 配置lda ...
- JavaScript的display属性
示例: <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" conten ...
- CIImage实现滤镜效果
Core Image also provides autoadjustment methods that analyze an image for common deficiencies and re ...
- 面向对象to1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 一个android参考网站,工具+源码
Android多渠道打包工具 https://github.com/wubo/apptools Android官方培训课程中文版(v0.9.4) http://hukai.me/android-t ...
- C语言状态机
转载声明 如果转载本博客内容,请联系869119842@qq.com,获得作者书面授权 前言 状态机的好处不用多说,自己百度去,但传统的编程模式,无论是C语言,或是硬件FPGA的Verilog都是采用 ...
- Android 标题栏菜单设置与应用(popupWindow的应用)
效果图
- AC自动机及trie图 pascal
; type data=record sum,failed:longint; son:array ['a'..'z'] of longint; end; ..maxn] of data; que:.. ...
- 全国大学列表文件(较新)+ nodejs导入mongodb数据库
直接上代码 'use strict' var fs=require('fs'), mongodb=require('mongodb').MongoClient, assert=require('ass ...