`
hillmover
  • 浏览: 34065 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Shell

阅读更多
#prompt
> echo $PS1
#searches manpage names and descriptions for regular expressions suplied as arguments.
> apropos music

> stat -c '%A %h %U %G %s %y %n' /tmp/studio
> find /tmp/ -name test.cpp -printf '%m %n %u %g %t %p'

#I won't pay $5 for coffee.
> echo 'I won'\''t pay $5 for coffee.'

#/etc/shells
> chsh

#GigaHerz = 1.93
> printf '%10.10s = %4.2f\n' 'GigaHerz' 1.92735

#1 STDOUT, 2 STDERR
> command 1> messages.out 2> messages.err
# command 1> messages.out 2>&1
> command &> messages.out

# braces { } used to group commands together
# with the braces used for grouping, any other changes that you make will be made to your current shell instance.
# The subshell is almost identical to the current shell's environment, but after it returns, its changes are never made to main shell instance.
> { comand1; command2; }  > all.out
> (comand1; command2) > all.out

# tee - read from STDIN and write to STDOUT & files
> command1 | tee /out.1 | command2

# $() encloses a command that is run in a subshell, and newlines in the output are replaced with $IFS, which is a space by default.
> rm $(find . -name '*.class')

Shorten this:
if [ $result = 1 ]; then
    echo "Result is 1; excellent."
    exit 0
else
    echo "Uh-oh, ummm, RUN AWAY!"
    exit 120
fi

into this:
[ $result =1 ] \
  && { echo "Result is 1; excellent."; exit 0; } \
  || { echo "Uh-oh, ummm, RUN AWAY!"; exit 120; }

standard output is buffered but standard error is unbuffered.

| or ! means do it anyway. Eg. in vi, q!. Without a !, the editor will complain if you havenot save your changes.
echo something >| output.file

<< says that we want to create such a temporary input source, and the following is just an arbitrary string to act as the terminator of the temporary input.

grep $1 <<\EOF      #grep $1 <<'EOF'
...
EOF

$? will be set with a non-zero value if the command fails. But be aware that you only  get one shot at reading the exit status.
somecommand;
if (($?)) ; then ...; else ...; fi
somecommand && ... || ...

# run cmd as deamon and ignore the SIGHUP
nohup cmd &

PROG=echo
HI=hello
$PROG $HI

for SCRIPT in /path/to/scripts/dir/*
do
    if [ -f $SCRIPT -a -x $SCRIPT ]
    then
        $SCRIPT
    fi
done

set, env, export -p : seeing all variable values.

$@, $* refers to all the arguments supplied on the command line.
"$*" gives the entire list inside one set of quotes, but "$@" returns not one string but a list of quoted strings, one for each argument.

for param in "$@"
do
    ...
done

${#} gives the number of parameters the script was invoked.
${#VAR} gives the length of the value in the variable VAR.
${VAR#alt} does a certain kind of substitution.

# the :- operator, says that if $1 is not set or is null then it will use what follows as the value.
VAR1=${1:-"defaultValue"}

# the := operator, is the same as :- operator. But the := will do an assignment as well as return the value on the right of the operator. The :- will do half  of that -- it just returns the value but doesn't do the assignment.
cd ${HOME:=/tmp}    # HOME eq. /tmp

#:? giving an error message for unset parameters
VAR1=${1:?"error message"}

inside ${ ... }         Action taken
name:number:number      Substring starting character, length
#name                   Return the length of the string
name#pattern            Remove (shortest) front-anchored pattern
name##pattern           Remove (longest) front-anchored pattern
name%pattern            Remove (shortest) rear-anchored pattern
name%%pattern           Remove (longest) rear-anchored pattern
name/pattern/string     Replace first occurrence
name//pattern/string    Replace all occurrences

array_1=(first second third)
${array_1[2]}   # $array_1 eq. ${array_1[0]}

a=$((2**8)) #eq. 256
let a/=5
# / is integer arithmetic, not floating point. Eg, 2/3 eq. 0.

if [ $# -lt 3]
then
    ...
fi

or alternatively:

if (( $# < 3))
then
    ...
elif (( $# > 3 ))
then
    ...
else
    ...
fi

if test $# -lt 3
then
    ...
fi

Option Description
-b File is block special device (for files like /dev/hda1)
-c File is character special (for files like /dev/tty)
-d File is a directory
-e File exists
-f File is a regular file
-g File has its set-group-ID bit set
-h File is a symbolic link (same as -L)
-G File is owned by the effective group ID
-k File has its sticky bit set
-L File is a symbolic link (same as -h)
-O File is owned by the effective user ID
-p File is a named pipe
-r File is readable
-s File has a size greater than zero
-S File is a socket
-u File has its set-user-ID bit set
-w File is writable
-x File is executable

FILE1 -nt FILE2
    ls newer than (it checks the modification date)
FILE1 -ot FILE2
    ls older than
FILE1 -ef FILE2
    Have the same device and inode numbers (identical file, even if pointed to by different links)

if [ -r $FILE -a -w $FILE ]
# use parentheses to get the proper precedence, as in a and (b or c), but be sure to escape their special meaning from the shell by putting a backslash before each or by quoting each parenthesis.
if [ -r "$FN" -a \( -f "$FN" -o -p "$FN" \) ]
# don't count on short circuits in your conditionals.
# it is important to put quotes around the "$VAR" expression because without them you syntax could be disturbed by odd user input. (like sql injection)

# the -eq operator for numeric comparisons and the equality primary = (or ==) for string comparisons.
Numeric  String  Meaning
-lt      <       Less than
-le      <=      Less than or equal to
-gt      >       Greater than
-ge      >=      Greater than or equal to
-eq      =, ==   Equal to
-ne      !=      Not equal to

# Testing with Regular Expressions:
# Ludwig Van Beethoven - 05 - "Leonore" Overture, No. 2 Op. 72.ogg
if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]]
then
    echo Track ${BASH_REMATCH[2]} is ${BASH_REMATCH[3]}
    mv "$CDTRACK" "Track${BASH_REMATCH[2]}"
fi
# For built-in array variable $BASH_REMATCH, the zeroth element ($BASH_REMATCH[0]) is the entire string matched by the regular expression. Any subexpressions are available as $BASH_REMATCH[1], #BASH_REMATCH[2], and so on.
#Matches are case-sensitive, but you may use shopt -s nocasematch (available in bash versions 3.1+) to change that. This option affects case and [[ commands.

while (( COUNT < MAX ))
do
    ...
done

while [ -z "$LOCKFILE" ]
do
    ...
done

while read lineoftext
do
    ...
done
#Any expression inside the parentheses is evaluated, and if the result is nonzero, then the result of the (( )) is to return a zero; similarly, a zero result returns a one.
# infinite loop
while ((1))
{
   ...
}

Eg.
$ svn status bcb
M bcb/amin.c
? bcb/dmin.c
? bcb/mdiv.tmp
A bcb/optrn.c
M bcb/optson.c
? bcb/prtbout.4161
? bcb/rideaslist.odt
? bcb/x.maxc
$
# delete ? files
svn status mysrc | \
while read TAG FN
do
if [[ $TAG == \? ]]
then
echo $FN
rm -rf "$FN"
fi
done

svn status mysrc | grep '^?' | cut -c8- | \
while read FN; do echo "$FN"; rm -rf "$FN"; done

for (( i=0, j=0 ; i+j < 10 ; i++, j++ ))
do
    echo $((i*j))
done

for i in 1 2 3 4 5 6 7 8 9 10   # for i in $(seq 1 1 10)
do
    echo $i
done

seq 1.0 .01 1.1 | \       #preferred approach for a really long sequence, as it can run the seq command in parallel with the while.
while read fp
do
echo $fp; other stuff too
done

case $FN in
    *.gif) gif2png $FN
    ;;
    *.png) pngOK $FN
    ;;
    *.jpg) jpg2gif $FN
    ;;
    *.tif | *.TIFF) tif2jpg $FN    #Use |, a vertical bar meaning logical OR.
    ;;
    *) printf "File not supported: %s" $FN
    ;;
esac

# get the content between line 555 and line 666 of file1.
sed -n "555,666"p file1

Well, that was then and this is now.
分享到:
评论

相关推荐

    B shell与 C shell的区别

    B shell与 C shell的区别 B shell和C shell都是Linux操作系统中的shell类型,它们之间存在一些关键的区别。 首先,让我们从B shell开始。B shell,全称为Bourne shell,是UNIX最初使用的shell。它在每种UNIX上都...

    Shell编程中文手册.pdf

    本手册涵盖了 Shell 编程的基础知识,包括 Shell 概述、Shell 解析器、Shell 脚本入门、Shell 中的变量等。 Shell 概述 Shell 是一种命令行接口,允许用户与操作系统进行交互。学习 Shell 编程可以让开发者更好地...

    LinuxShell脚本学习基础视频

    资源名称:Linux Shell脚本学习基础视频资源目录:【】11a00d99b60c4e2eba3440b8aa3a6bdd【】linux_shell脚本编程_01认识shell,如何编写shell脚本和执行【】linux_shell脚本编程_02vivim简单的常用操作【】linux_...

    C语言中文网shell脚本教程

    **C语言中文网shell脚本教程** 这是一份关于Shell脚本编程的离线学习资料,包含了一系列HTML文件,旨在帮助用户深入理解并掌握Linux Shell脚本编程技术。以下是其中涉及的一些关键知识点: 1. **Shell命令的本质**...

    Shell脚本中获取进程ID的方法

    提问: 我想要知道运行中脚本子shell的进程id。我该如何在shell脚本中得到PID。 当我在执行shell脚本时,它会启动一个叫子shell的进程。作为主shell的子进程,子shell将shell脚本中的命令作为批处理运行(因此称为...

    Windows Shell 编程.pdf

    Windows Shell 编程.pdf 看过一些对windows 外壳的扩展程序,在使用上一般都是直接利用windows的外壳API做一些工作,因为外壳操作需要一些比较专业的知识,因此,大部分编程人员特别是使用集成编程环境的程序人员对...

    SHELL十三问,PDF

    此外,Shell并非固定不变的,用户可以根据个人需求选择不同的Shell类型,常见的Shell包括Bourne Shell (`sh`)、Bourne-Again Shell (`bash`)、C Shell (`csh`)、T C Shell (`tcsh`) 和 Korn Shell (`ksh`)等。...

    大数据技术之Shell.docx

    "大数据技术之Shell" 本资源是关于大数据技术中的 Shell 技术的详细文档,涵盖了 Shell 的概述、Shell 脚本入门、变量等方面的知识点。 章节 1:Shell 概述 Linux 提供的 Shell 解析器有多种,包括 /bin/sh、/bin...

    shell编程入门经典--LINUX与UNIX Shell编程指南 (中文pdf版)

    《LINUX与UNIX Shell编程指南》是一本专为初学者设计的shell编程教程,它深入浅出地介绍了在Linux和UNIX系统中如何使用Shell进行高效自动化任务处理。Shell编程是Linux和UNIX系统中的核心技术,它允许用户通过命令行...

    在MCU上运行的简单控制台shell

    为了方便地与这些设备交互并进行调试,开发人员常常会实现一个控制台shell,它允许通过串口或其他通信接口发送命令到MCU并接收响应。本文将深入探讨如何在MCU上运行的简单控制台shell及其相关知识点。 **控制台...

    linux_shell实例精解

    Linux Shell是Linux操作系统中的一种命令解释器,它提供了一个用户与操作系统内核交互的界面,使得用户可以通过文本命令行执行各种操作。Shell脚本则是一种编程语言,它允许用户编写包含一系列命令的程序,实现自动...

    Visual Studio 2013 Shell 下载

    ### Visual Studio 2013 Shell 知识点解析 #### 一、Visual Studio Shell 概述 在软件开发领域,Visual Studio Shell 是一个极为重要的工具。它由微软推出,旨在为开发者提供一个灵活且可扩展的基础平台。通过这个...

    shell脚本与Makefile区别.docx

    "shell脚本与Makefile区别" shell 脚本和 Makefile 是两个不同的工具,它们都用于自动化构建和编译过程,但是它们有很大的不同之处。本文将详细介绍 shell 脚本和 Makefile 的区别。 首先,shell 脚本和 Makefile ...

    放在U盘根目录就可运行的EFI shell

    EFI Shell是一种基于EFI(Extensible Firmware Interface)标准的操作系统环境,它允许用户在UEFI固件上执行命令行工具和应用程序。EFI是Intel提出的一种新型固件接口,用于替代传统的BIOS,提供更强大、更灵活的...

    Linux 下 Shell的工作原理

    Linux中的Shell是一个至关重要的组成部分,它是用户与操作系统交互的接口,扮演着命令解释器的角色。在Linux系统中,默认的Shell通常是Bash(Bourne-Again SHell),它继承了Bourne shell的功能并增加了许多扩展特性...

    shell 13问 简体中文版.pdf

    ### shell 13问知识点详解 #### 一、Shell是什么? **知识点1:Shell的定义与作用** - **定义**:Shell是一种用户与计算机系统之间的交互界面,它充当了一个命令解释器的角色,能够将用户的命令转化为系统能识别的...

    U盘版EFI SHELL

    "U盘版EFI SHELL" 是一种用于UEFI(统一可扩展固件接口)环境的实用工具,它允许用户在没有操作系统的情况下通过U盘启动计算机并执行命令行操作。UEFI Shell是UEFI环境中的一种命令行接口,它提供了一系列内置的命令...

    Shell源码(Shell源码)

    Shell是Unix和Linux操作系统中的命令解释器,它提供了一个用户与操作系统内核交互的界面,允许用户通过输入命令来执行系统功能。Shell不仅是一个命令行接口,还是一个强大的编程语言,用户可以编写脚本来自动化一...

Global site tag (gtag.js) - Google Analytics