zoukankan      html  css  js  c++  java
  • shell专题(三):Shell脚本入门

    1脚本格式

    脚本以#!/bin/bash开头(指定解析

    2第一个Shell脚本helloworld

    1)需求:创建一个Shell脚本,输出helloworld

    2案例实操:

     

    [atguigu@hadoop101 datas]$ touch helloworld.sh
    [atguigu@hadoop101 datas]$ vi helloworld.sh
    
    在helloworld.sh中输入如下内容
    #!/bin/bash
    echo "helloworld"

     

    3脚本的常用执行方式

    种:采用bashsh+脚本相对路径或绝对路径(不用赋予脚本+x权限)

    sh+脚本的相路径

     

    [atguigu@hadoop101 datas]$ sh helloworld.sh 
    Helloworld

     

    sh+脚本的绝对路径

    [atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh 
    helloworld

    bash+脚本的相对路径

    [atguigu@hadoop101 datas]$ bash helloworld.sh 
    Helloworld

    bash+脚本的绝对路径

    [atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh 
    Helloworld

    种:采用输入脚本的绝对路径或相对路径执行脚本必须具有可执行权限+x

    a)首先要赋予helloworld.sh 脚本的+x权限

     

    [atguigu@hadoop101 datas]$ chmod 777 helloworld.sh

     

    b)执行脚本

    相对路径

     

    [atguigu@hadoop101 datas]$ ./helloworld.sh 
    Helloworld

     

    绝对路径

    [atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 
    Helloworld

    注意:第一种执行方法,本质bash解析器帮你执行脚本,所以脚本本身不需要执行权限。二种执行方法,本质是脚本需要自己执行需要执行权限。

    3Shell脚本:多命令处理

    1)需求

    /home/atguigu/目录下创建一个banzhang.txt,banzhang.txt文件中增加“I love cls”

    2)案例实操:

     

    [atguigu@hadoop101 datas]$ touch batch.sh
    [atguigu@hadoop101 datas]$ vi batch.sh
    
    在batch.sh中输入如下内容
    #!/bin/bash
    
    cd /home/atguigu
    touch cls.txt
    echo "I love cls" >>cls.txt

     

    1.脚本格式脚本以#!/bin/bash开头(指定解析器)2.第一个Shell脚本:helloworld(1)需求:创建一个Shell脚本,输出helloworld(2)案例实操:

  • 相关阅读:
    解决listview在添加项闪烁的问题
    对于水晶报表,我欲哭无泪
    遗憾4年的.net学习和工作
    (转)字符编码笔记:ASCII,Unicode和UTF8
    关于将桌面扩展到监视器的问题 extended my windows desktop onto this monitor
    在win下mb_convert_encoding未定义的问题最终解决方案
    插入数据后, 获取该记录id的方法
    Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
    感冒怎么办,少听音乐防止感冒 生活至上,美容至尚!
    2招按摩轻松解除黑眼圈 生活至上,美容至尚!
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13216775.html
Copyright © 2011-2022 走看看