前十几天的笔记

第一天
看英文书 学习Linux下到编程 有意义!

今天重温了GCC的基本操作  还了解到Linux居然可以在2M RAM,且没有硬盘的情况下运行,厉害!所有说硬盘并不是启动计算机必不可少的硬件。

第二天
1. 应用程序通常放置于
   /usr/bin         /usr/local/bin    /opt中

   良好的习惯:将零售商提供的文件等放在 /usr/local 中,因为当升级操作系统时,只有 /usr/local 需要被保留。

2. 头文件通常放置于 /usr/include 中。如:
   C语言到头文件一般在 /usr/include
   X Window System的头文件在 /usr/include/X11
   C++的在 /usr/include/c++

3. 库文件通常放置于 /lib 和 /usr/lib 中
  库文件中以 .a 结尾的是传统的静态库
           以 .so结尾的是共享库
  对于C语言而言,如果使用C即时库以外的库文件,需要使用 -l 选项予以指名。如
  $ gcc -o x11fred  x11fred.c -L/usr/openwin/lib -lX11

例子:
    一    新建四个文件
    $vim bill.c
    #include <stdio.h>
    void bill(char *arg)
    {
      printf("bill: you passed %s/n",arg);
    }
    
    $vim fred.c
    #include <stdio.h>
    void fred(int arg)
    {
      printf("fred: you passed %d/n",arg);
    }

    $vim lib.h
    void bill(char *);
    void fred(int);

    $vim program.c
    #include "lib.h"
    int main()
    {
      bill("Hello World!");
    }

    二    生成bill.o  fred.o  program.o
    $gcc -c bill.c fred.c program.c
    三    连接 生成静态链接库 libfoo.a
    $ar crv libfoo.a bill.o fred.o
    $ranlib libfoo.a
    四    生成可执行文件 并显示结果
    $gcc -o program program.o libfoo.a
    $./program
       
4. 帮助系统  man 和 info
    如 : $man ls

5. shell中
    > 用于输出重定向 如: $ls -la > lsoutput.txt
    >> 用于追加输出  如: $pa >>lsoutput.txt

第三天
1. 输出有3个descriptor,“0”代表标准输入,“1”代表标准输出,“2”代表标准错误输出。例如:
    $kill -HUP 1234 >killout.txt 2>killerr.txt
    $kill -1 1234 >killouterr.txt 2>&1


2. 管道Pipe的使用
    $ps | sort > pssort.out
    注:绝对不要在同一串命令中使用同一个文件名两次,如下
    $cat mydata.txt | sort | uniq | > mydata.txt
    将得到空文件,因为在cat文件以前命令已经用一个空文件覆盖了原有的文件

第四天
通配符wildcard有  "*","?"。在shell中使用花括号brace "{}"如:
    $ls my_{finger,toe}s    将列出两个文件my_fingers 和 my_toes

第五天
1. Linux和NUIX系统一般很少使用扩展名或后缀来决定文件的类型。
   可以使用file命令来检测一个文件是不是scripts。如:
    $file first            first是一个文件的名称
    $chomod +x first
    $./first            如果你省略第二步会出错,因为文件还不是可运行的。


2. shell变量
    A.Linux系统中,是大小写敏感的;
    B.变量的使用不用事先定义;
    C."$"的含义:我们要访问一个变量的内容,要加上$符号。如
        $salutation=7+3        注意:等号两边不能有空格符号
        $echo $salutation
        7+3                注意:结果并不是10
    D.输入/为变量赋值,可以用read命令,在屏幕上显示可以用echo。如:
        $read sa    即为参数sa赋值,之后终端等待用户输入值,输入一般不使用引号
    E.注意引号的使用:
        [liusj@localhost ~]$ sa="who is this?"
        [liusj@localhost ~]$ sb="$sa"    双引号中的$仍起作用
        [liusj@localhost ~]$ echo $sb
        who is this?

        [liusj@localhost ~]$ sc='$sa'    单引号中的文本被直接复制,$不起作用
        [liusj@localhost ~]$ echo $sc
        $sa

        [liusj@localhost ~]$ sd="/$sa"    "/"反斜杠为转义符号,在双引号中起作用
        [liusj@localhost ~]$ echo $sd    将$符号转义
        $sa

        [liusj@localhost ~]$ se='/$sa'    "/"反斜杠在单引号中也失去作用
        [liusj@localhost ~]$ echo $se
        /$sa


    F.    $0 表示shell文件的名称
        $1 , $2 , ...... 表示第1,2等等个参数
        $* 表示所有参数的序列
    所以选择某个shell程序的参数的方法如下:
        $ set foo bar baz
        $ echo "$*"
        foo bar baz


3. which:shell命令,用于显示一个shell指令的详细路径。如:
    $which test
    /usr/bin/test
  test在Linux中是一个shell指令,所有如果用户定义一个test文件并试图运行,则可能会引起不必要的冲突。


4. 条件判断
    test 或者 [ 命令。如:
        if test -f fred.c            if [ -f fred.c ]      此时[,]符号前后都要有空格
          then                            then
          echo "Yes!"               echo "Yes!"
        fi                                  fi
    上文中左右等价。右边的更像C语言中的 if(){} 判断语句。又例如:
        if [ -f /bin/bash ] ; then
            echo "file /bin/bash exists"
        fi

        if [ -d /bin/bash ]
        then
            echo "/bin/bash is a directory"
        else
            echo "/bin/bash is NOT a directory"
        fi


第七天
1. for循环
        for variable in values        for file in $(ls f*.sh); do
        do                                             lpr $file
               statements                      done
        done


2. while循环
        while condition ; do        while [ “$foo” -le 20 ] ;do
            statements                      echo “Here we go again”
        done                                     foo=$(($foo+1))        注意这个表示方法
                                                   done
第八天
3. until循环
        until condition
        do
              statements
        done


4. case
        case variable in
              pattern [ | pattern] ...) statements;;
              pattern [ | pattern] ...) statements;;
             ...
        esac
    要注意每条语句后的双分号";;"。如:
        echo “Is it morning? Please answer yes or no”
        read timeofday
        case “$timeofday” in
                yes | y | Yes| YES ) echo “Good Morning”;;         用"|"分隔多个项目
                [nN]*) echo “Good Afternoon”;;                             用"[]"包含大小写;巧妙使用通配符"*"
                *) echo “Sorry, answer not recognized”               此处通配符作为默认处理选项
                exit 1;;                                                                         esac之前的双分号其实可以省略,但是为了保持良好习惯,一般写上
        esac
        exit 0


5. AND和OR序列
        statement1 && statement2 && statement3 && ...
        statement1 || statement2 || statement3 || ...


6. touch命令
         $touch file_one
        将检查当前目录下有无该文件,没有则新建该文件


7. 函数
    作为一个选择,你可以将一个大的Script文件分为很多小的文件,但缺点是会大大降低速度。所有一般使用函数而不是分解文件。
    在调用一个函数之前必须要定义他。方便的是如变量一样,不需要对其进行声明。
    函数可以有参数;有返回值 用return命令;有局部变量 用local标识。
    函数的变量如果没有申明,则都是全局变量,除非用local表示为局部变量。


8. break          用法跟C语言中一样
9. :        ":"命令符,可以当作"true"的别名。 比如:while : 可以等价于 while true
10. continue    与C语言中一致
11. echo     与之相应的也有printf


12. eval    可以用于对参数赋值
    foo=10            foo=10
    x=foo                x=foo
    y='$'$x            eval y='$'$x
    echo $y            echo $y
    第一个的结果是$foo,而第二个的结果是10!


13. exec    该命令有两个用法
    最一般的用法是用来将当前shell替换为一个不同的程序。如:
        exec wall "Thanks"
    第二种用于改变当前的文件解释器。用得很少。
14. exit n    出口代码相关
15. export

第九天
16. expr
17. printf
    $ printf “%s %d/t%s” “Hi There” 15 people
    Hi There 15   people
18. return
19. set  为shell设置参数变量
20. shift
21. trap    trap command signal
22. unset     从用户环境中清除变量或函数


23. find                  find [path]   [options]     [tests]     [actions]
                          # find     /     -mount          -name wish       -print
    
    Option                     Meaning
     -depth                       Search the contents of a directory before looking at the directory itself.

     -follow                       Follow symbolic links.

    -maxdepths N           Search at most N levels of directory when searching.

    -mount (or -xdev)     Don’t search directories on other file systems.

    Test                          Meaning
    -atime N                     The file was last accessed N days ago.

    -mtime N                     The file was last modified N days ago.

     -name pattern           The name of the file, excluding any path, matches the pattern pro-
                                          vided. To ensure that the pattern is passed to find, and not evalu-
                                          ated by the shell immediately, the pattern must always be in quotes.
                             
    -newer otherfile          The file is newer than the file otherfile.

    -type C                         The file is of type C, where C can be of a particular type; the most
                                          common are “d” for a directory and “f” for a regular file. For other
                                          types consult the manual pages.

    -user username         The file is owned by the user with the given name.

    可以联合tests参数使用一些操作符,如:
    Operator, Short Form     Operator, Long Form     Meaning
      !                                -not                                           Invert the test.

      -a                               -and                                           Both tests must be true.

         -o                               -or                                        Either test must be true.

    Action                          Meaning
    -exec command          Execute a command. This is one of the most common actions See the
                                           explanation following this table for how parameters may be passed to
                                           the command.

    -ok command             Like -exec, except that it prompts for user confirmation of each file on
                                          which it will carry out the command before executing the command.

    -print                              Prints out the name of the file.

    -ls                                   Uses the command ls -dils on the current file.


第十一天
24. grep    grep [options] PATTERN [FILES]
    find是在文件系统中查找某文件,而grep是在文件中查找对应的字符串

第十三天
25. $(command)  或者 `command`
    用于捕获command命令的执行。后者是比较老的形式,如果需要移植性高,可用后者。$(command)的结果是来自command的输出。并不是返回命令的状态,而是输出序列。如:
    $ echo The current directory is $PWD
    The current directory is /home/liusj
    $ echo The current users are $(who)
    The current users are liusj :0 2009-08-10 22:59 liusj pts/0 2009-08-10 23:26 (:0.0)
    当前目录是shell的环境变量,所有不用该结构;而$(who)则用于把当前用户的结果显示出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值