Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66857
  • 博文数量: 36
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-04 12:39
文章分类

全部博文(36)

文章存档

2010年(1)

2009年(35)

我的朋友
最近访客

分类: LINUX

2009-04-25 18:53:08

了解了基本的语法结构进行一般的任务处理就很容易了,读别人的代码也容易的多。
从windows想linux转,学的小东西海真的不少,现在感觉可以告一段落了,相关的东西都知道是怎么回事了,以后碰到的时候再查查资料即可。
     
************************

注意空格
x=3,=号两边没有空格,有了就不对

      ************************
;表示语句结束
;;表示直接跳转出当前代码block,相当于break

      ************************
测试条件的形式如:
if [ -z "" ]; then
...
fi
[]是以空格开始和结尾的

其中-z表示测试类型,这里就是试图测试后面的字符串长度是否为0,

      ************************
其余测试类型如下:
1) file relatted test
    -b file / exist and block file
    -c file /exist and character file
    -d file /exist and directory file
    -e file /test exist only
    -f file /exist and formal regular file(正常文件)
    -g file /exist and SGID setting file
    -h file /exist and symbol link file
    -k file /exist and sticky setting file
    -p file /exist and named pipe file
    -r file /exist and can read (not readonly) file
    -s file /exist and size > 0 file ( the file contains at least one bit)
    -u file /exist and SUID setting file
    -w file /exist and writable file
    -x file /exist and executable file
    -o file /exist and valid UID file

2) string test
    -z "string" true if len() == 0
    -n "string" true if len != 0
    "string" = "string" (note, = but == )
    "string" != "string"

    the above = and != is case-sensitive

3) integer comparision, suppose type(a) = int, type(b) = int
    a -eq b /true if a == b
    a -ne b /true if a != b
    a -lt b /trua if a < b
    a -le b / true if a <= b
    a -gt b /true if a > b
    a -ge b /true if a >=b

      ************************
赋值的时候不需要$x,只有引用的时候才需要,所以
$x=`echo "$x+1" | bc`是错的,
x=`echo "$x+1" | bc`是对的,
假设x=3的话,则echo “$x+1" is
3+1
this expression was feed into bc (bc is calculator), we get 4
`x=4` was executed, because it was wrapped with ``, note ` is not ', ` key is in upper-left of keyboard

      ************************
各种循环条件
#while
x=0
while [ $x -lt 10 ]
do
    echo $x
    x=`echo "$x+1" | bc`
done

echo '-------------'
#case
x=2
case $x in
    1) echo "1";;
    2) echo "2";;
    *) echo "none";;
esac

echo '-------------'
#until
until [ $x -eq 5 ]
do
    echo $x
    x=`echo "$x+1" | bc`
done

echo '-------------'
for i in $HOME/.bash*
do
    echo $i
done

break 可以加整数表示退出的层数,continue语义同c/c++等

      ************************
变量代换
下面的所有语句都有两个方面的影响,一是表达式返回的值是什么,二是变量本身的值是否发生了改变
${para:-word} if para is null or 0, expression return word, otherwise, expression return $para, para keep unchanged
${para:=word} if para is null or 0, expression return word +and+ para was assigned with word, otherwise, expression return $para
${para:?word} if para is null or 0, expression return word and word output to stdout, otherwise, expression return $para, para keep unchanged
${para:+word} if para is null or 0, expression return null; otherwise, expression return word, and para was assigned as word

      ************************
子函数声明,这个普通的C/C++类似
setPath(){
    PATH=${PATH:="/sbin:/bin"};
    for _DIR in "$@"
    do
        if [ -d "$_DIR" ]; then
            PATH="$PATH":"$_DIR";
        fi
    done
    export PATH
    unset _DIR
}

      ************************
shell中可以使用环境变量来方便的进行信息共享

      ************************
一些特殊的shell命令
: 什么都不做的命令,等于C中的;
ls |xargs -n 1 echo + xargs将后面的所有输入当做参数,最后再加上前面的命令的输出,-n 1表示一次只处理一条输出,一般用于参数太长

      ************************
调试
bin/sh -n 读所有命令但不执行
        -v 读入并显示所有行
        -x 执行命令时显示所有命令和他们的参数
阅读(656) | 评论(6) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-10-13 10:21:43

the following article is from http://www.kaiyuanba.cn/html/1/131/137/4108.htm, which own full right. just for easy access, so i copy here. Bash shell 在当今的许多 Linux® 和 UNIX® 系统上都可使用,是 Linux 上常见的默认 shell。Bash 包含强大的编程功能,其中包括丰富的可测试文件类型和属性的函数,以及在多数编程语言中可以使用的算术和字符串比较函数。理解不同的测试并认识到 shell 还能把一些操作符解释成 shell 元字符,是成为高级 shell 用户的重要一步。这篇文章摘自 developerWorks 教程 LPI 102 考试准备,主题 109: Shell、脚本、编程和编译,介绍了如何理解和使用 Bash shell 的测试和比较操作。 这个技巧解释了 shell 测试和比较函数,演示了如何向 shell 添加编程功能。您可能已经看到过使用 && 和 || 操作符的简单 s

raymond19842009-09-05 22:28:41

字符串长度,子字符串等等 http://bbs.linuxsky.org/thread-3367-1-1.html

raymond19842009-09-05 21:35:48

这是对变量子串替换的经典实例: 假設我們定義了一個變量為: file=/dir1/dir2/dir3/my.file.txt 我們可以用 ${ } 分別替換獲得不同的值: ${file#*/}:拿掉第一條 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt ${file##*/}:拿掉最後一條 / 及其左邊的字串:my.file.txt ${file#*.}:拿掉第一個 . 及其左邊的字串:file.txt ${file##*.}:拿掉最後一個 . 及其左邊的字串:txt ${file%/*}:拿掉最後條 / 及其右邊的字串:/dir1/dir2/dir3 ${file%%/*}:拿掉第一條 / 及其右邊的字串:(空值) ${file%.*}:拿掉最後一個 . 及其右邊的字串:/dir1/dir2/dir3/my.file ${file%%.*}:拿掉第一個 . 及其右邊的字串:/dir1/dir2/dir3/my 記憶的方法為: # 是去掉左邊(在鑑盤上 # 在 $ 之左邊) % 是去掉

raymond19842009-08-08 18:30:00

非常容易看懂的awk使用方法,使用linux的还是应该会awk,非常方便的。http://fanqiang.chinaunix.net/program/shell/2005-03-30/3068.shtml 打造VIM 成IDE http://blog.csdn.net/wooin/archive/2007/10/31/1858917.aspx,我就是按照这上面说的弄的,很不错的。

raymond19842009-06-09 14:37:04

[ 原来是linux的一个命令,以前还真不知道,这下应该好理解[为什么加空格了,命令与参数/其他命令间需要分割符。 这和C/C++之类的太不一样了呵呵