分类目录归档:Linux shell

Linux Shell参数替换

Bash中的$符号的作用是参数替换,将参数名替换为参数所代表的值。对于$来说,大括号是可选的,即$A和${A}代表同一个参数。

${}带冒号的有下面几种表达式:

${parameter:-word}

如果parameter为null或者未设置,整个参数替换表达式值为word

${parameter:=word}

如果parameter为null或者未设置,整个参数替换表达式值为word,并且parameter参数值设置为word

${parameter:?word}

如果parameter为null或者未设置,则打印出错误信息。否则,整个参数替换表达式值为$parameter

${parameter:+word}

如果parameter不为null或者未设置,则整个参数替换表达式值为word

${parameter:offset}

${parameter:offset:length}

继续阅读

linux shell tips

Bash Bits: Check if item is in array

This is a simple function which helps you find out if an (non associative) array has an item. It allows you to call the function with just the array name, not ${arrayname[@]}. It returns 1 if the item is in the array, and 0 if it is not. 继续阅读

Awk1line中文版

AWK单行脚本快速参考 2008年4月28日
编辑: Eric Pement eric [at] pement.org 版本 0.26
翻译: 董一粟 yisudong [at] gmail.com
最新英文版本文件发布在以下地址:
http://www.pement.org/awk/awk1line.txt
最新中文翻译版本文件发布在以下地址:
http://ximix.org/translation/awk1line_zh-CN.txt
翻译问题欢迎联系我:
http://hi.baidu.com/ximix/blog/item/e9334cc2204fa330e5dd3bf2.html

用法:

Unix: awk ‘/pattern/ {print “$1”}’ # 标准 Unix shell环境
DOS/Win: awk ‘/pattern/ {print “$1”}’ # DJGPP 可编译通过
awk “/pattern/ {print \”$1\”}” # GnuWin32,UnxUtils,Mingw环境

需要特别注意的是,DJGPP编译器可以允许awk脚本使用Unix的引号语法
‘/like/ {“this”}’。但是,用户必须知道在DOS/Windows环境下,使用CMD.EXE或者
COMMAND.COM程序的话,单引号并不能保护重定向符号(<, >)和管道(|)。
如果使用双引号的话,在DOS/CMD命令解释器下的特殊符号和他们的特殊含义都
会被忽略。如果你的命令提示符是bash、ksh或者其他的Unix终端,单引号和双引号
会沿用Unix标准的用法。 继续阅读

How to Write Linux Init Scripts Based on LSB Init Standard

LSB stands for Linux Standard Base.

LSB was started by Linux Foundation to reduce the difference between several Linux distributions, and thereby reducing the cost involved in porting between different distributions. Init scripts are one among them to be standardized.

In this article, we will see how to write an Init script that conforms to LSBInit Standard.

Init scripts are used to start|stop a software|service. For example, if you are using postgresql software, we will have a Init script named ‘/etc/init.d/postgresql’ which can be used to ‘start|stop|restart|reload|force-reload|status’. 继续阅读

General Shell Tips

Automatic Command Completion

Use the TAB key and bash will attempt to complete the command for you automatically. You can use it to complete command (tool) names. You can also use it when working with the file-system, when changing directories, copying files et cetera. 继续阅读

shell中的参数扩展及特殊变量

echo $SHLVL  该变量包含当前工作的shell level

$*  所有参数列表 受IFS控制

PS1 为提示符,可进行设置 PS1=”[\u@\h \W]\$”
PS2 为换行时的那个符号如 aa ‘换行后默认会出现>我们可以进行修改 PS2=”换成你喜欢的
IFS 为变量的分隔符,默认是空格 继续阅读

SHC:Generic shell script compiler

经常使用脚本,但是在脚本中写入密码明文很不安全,所以考虑加密问题。当前比较普遍、简单的方法为使用shc程序,把脚本使用RC4加密算法再编译一次。
shc的主页:
http://www.datsi.fi.upm.es/~frosal/
当前最新为http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8 .7.tgz
具体操作如下:
# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8 .7.tgz
# tar zxvf shc-3.8.7.tgz
# cd shc-3.8.7
# make test
# make strings
到此,目录下已经有了shc的程序,可以直接使用,如果希望使用方便也可以在安装一下。
# make install
安装完成,非常简单。使用也非常简单。
# ./shc -v -f myshell.sh
编译完成后会产生两个myshell.sh.x和myshell.sh.x.c。myshell.sh.x为二进制文件,
myshell.sh.x.c为C源码文件。

继续阅读

Linux Shell Scripting Tutorial (LSST) v2.0

Introduction

This tutorial is a beginners handbook for new Linux users / Sys admins and school students studying Linux or computer science. This book is licensed under“Creative Commons Attribution Noncommercial Share Alike 3.0 Unported”.

Linux Shell Scripting Tutorial (LSST) v2.0

Written by Vivek Gite. Copyright 1999-2010 Vivek Gite and its contributors. Some rights reserved. 继续阅读

linux shell环境减号”-“的用途

连字符:年轻人称之为dash,年长者称之为minus,英文名称hyphen(连字符),minus sign(减号),或虚线

减号”-”就是代表标准输出/标准输入, 视命令而定. “-”代替stdin和stdout的用法,stdin就是标准输入,stdout就是标准。 继续阅读

shell how tos

shell how to主要收集在shell使用过程中遇到的经典案例,或疑难实现以及shell使用技巧等。

1、 shell如何实现逐行读取文件

#!/bin/sh
filepath="/home/test/test.txt"
while read LINE
do
user=`echo $LINE | awk '{print $1}'`
pwd=`echo $LINE | awk '{print $2}'`
done <$filepath
继续阅读