zoukankan      html  css  js  c++  java
  • Shell基本使用

    shell说明

    Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

    Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

    Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

    shell使用

    第一个shell脚本

    echo "Hello world"
    

    可以创建test.sh文件,把上面的脚本写进去

    touch test.sh
    vi test.sh
    echo "Hello world"
    

    再执行./test.sh

    分配权限再执行:

    chmod 777 test.sh
    ./test.sh
    

    变量

    声明变量赋值

    myname="coydone"
    echo $myname
    echo ${myname}
    

    printf

    语法:

    printf  format-string  [arguments...]
    format-string: 为格式控制字符串
    arguments: 为参数列表。
    
    printf "%-10s %-8s %-4s
    " 姓名 性别 体重kg  
    printf "%-10s %-8s %-4.2f
    " 郭靖 男 66.1234 
    printf "%-10s %-8s %-4.2f
    " 杨过 男 48.6543 
    printf "%-10s %-8s %-4.2f
    " 郭芙 女 47.9876 
    

    循环

    • for

    语法:

    for var in item1 item2 ... itemN
    do
        command1
        command2
        ...
        commandN
    done
    

    案例:

    for loop in 1 2 3 4 5
    do
        echo "The value is:"$loop
    done
    for str in 'This' 'is' 'a' 'string'
    do
        echo $str
    done
    
    • while

    语法:

    while condition
    do
        command
    done
    

    案例:

    int=1
    while(( $int<=5 ))
    do
        echo $int
        let "int++"
    done
    
    • 死循环

    语法:

    while :
    do
        command
    done
    
    while true
    do
        command
    done
    
    for (( ; ; ))
    

    更多shell编程可以查看菜鸟教程:http://www.runoob.com/linux/linux-shell.html

    coydone的博客
  • 相关阅读:
    Hrbust-1492 盒子(二分图最大匹配)
    数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
    HDU 1710 二叉树遍历
    HDU 2891
    HDU 2895 贪心 还是 大水题
    POJ 2896 另解暴力
    POJ 2896 AC自动机 or 暴力
    HDU 1714 math
    POJ 1328 贪心
    POJ 2109 巧妙解法
  • 原文地址:https://www.cnblogs.com/coydone/p/13920421.html
Copyright © 2011-2022 走看看