Under Linux shell programming learning

Under Linux shell programming learning

1. digression

  • Since the first written blog (on the Blue Bridge Cup), and a week later, had planned to be at least one week to write a blog to write down their own progress in a week of (learning), and thus facilitate their future Recalling the study, but in the end he is still delayed all kinds of things that he sometimes just do not want to learn a whole, do not want to go and get this, I feel typesetting, typing, looking particularly troublesome time-consuming material, preferring to sit and do not want to go and get, to no longer time dragged on, it is going to get this up, and sometimes found the whole thing was not particularly troublesome, but lack the courage to try, do not bother their own imagination, it is not going to get the results of their own time to do it personally, but also found not so much trouble, but do it very feeling that their own progress, and talked a lot of nonsense, in short, life or must have the courage to try, so will have their own progress .
  • By the way, I saw a share of the day's program, including learning to relax and feel good;
    Here Insert Picture Description

2.shell learning program

  • echo
    • Similar to the C language printf, it is what the printed output.
     zgq@123:~$ echo "you are a lovely girl!"
     you are a lovely girl!               ##直接在终端就输出.
    
    • If you want to output the value of variables, of course, certainly also possible, for in printf you can know.
     zgq@123:~$ echo &date "+%B%d%A"  ##date是变量,然后以月日星期的格式输出
     四月08星期一                      ##终端输出
    
    • Of course, if you want to direct output date, also possible, in its default format, the only note is that when the output variables need to add the "&" symbol .
    zgq@123:~$ echo &date
    2019年 04月 08日 星期一 22:40:12 CST
    
    • In addition, after, or you may not add the "&" symbol, directly by reverse quotes directly linked, may be directly output.
     zgq@123:~$ echo `date`
     2019年 04月 08日 星期一 22:40:19 CST
    
    • This is in direct output terminal command, of course, can also create shell command file, and then set the file permissions, and then run it.
        #!/bin/sh
        echo "Mr.$USER,Today is:"
        echo `date "+%B%d%A"`
        echo "wish you a lucky day!"
    
     ## 然后设置文件权限,用到chmod指令,设置该文件 可读可写可操作,然后运行该shell文件,即可得到运行结果。
     zgq@123:~~/bin$ chmod 700 FirstShell 
     zgq@123:~/bin$ ./FirstShell   
    
     Mr.zgq,Today is:
     四月08星期一
     wish you a lucky day!
    
    • About chmod command may be said here about the usage of chmod [ABC] file or directory
      a, b, c, representing the user, group, other permissions and r = 4, w = 2, x = 1, the representative read , write, execute permissions, 0 is not the authority.
      For example chmod 666 FirstShell bash provided the file user, group, other permissions are read and write, but can not operate.
  • if
    the judge sentences, the main difference is the way of judgment, -r expressed as true, -d represents the directory is true and so on.
     #! /bin/bash
     echo "Please enter the directory name of file name"
     read dd
     if [ -r $dd ]
     then
     ls $dd
     else
    echo "input error"
    fi
    
    zgq@123:~/bin$ chmod 700 shell212
    zgq@123:~/bin$ ./shell212
    Please enter the directory name of file name
    ZGQ    ##输入文件夹名称,并且可读
    Add.c  Add.o	hello.h  Makefile  print.h  test    test.o   ##列出文件夹下的名称
    Add.h  hello.c	hello.o  print.c   print.o  test.c
    
  • for
    the C language for use in a similar, the following function to calculate a 1-1 / 2 + 1 / 3-1 / 4 + 1 / 5- ... - / + 1 / n, n input by the user
    #! /bin/bash
    read n
    total=0.0000
    an=0.0000
    val=0
    begin=$(date +%s)       #记录开始时间
    for((num=1;num<=$n;num++));
    do 
      i=$num        ##循环变量
      if [ $i != 0 ]  
      then
        an=`echo "scale=4;1.0000/$i" | bc`        ##bc用来表示计算小数,scale表示计算保留的小数位数,
        val=`expr $i % 2`                         ##判断是否为偶数,分析上面题目可知,可知偶数为-,奇数为+
        if [ $val == 0 ]                          #若为偶数,则-,否则为+
        then
          total=`echo "scale=4;$total-$an" | bc`
        else
          total=`echo "scale=4;$total+$an" | bc`
        fi
    fi
    done
    end=$(date +%s)
    cost=$(($end - $begin))    #输出计算时间
    echo $total                #计算处结果
    echo "花费的时间是$cost s"
    
    zgq@123:~/bin$ ./shell218
    500
    .6909
    花费的时间是4 s
    
Released seven original articles · won praise 13 · views 2073

Guess you like

Origin blog.csdn.net/qq_34430371/article/details/89098464