美文网首页
linux学习笔记---5:vim编辑器及命令执行,头命令;标准

linux学习笔记---5:vim编辑器及命令执行,头命令;标准

作者: javen_spring | 来源:发表于2020-05-25 18:03 被阅读0次

    vim编辑器与linux脚本

    • 头命令:linux脚本的头命令:#!/bin/bash ---指定linux脚本的解释器
    linux脚本的解释器
    • 在没有指定解释器(头命令)的情况,用 bash test.sh 命令运行脚本;在有头命令指定脚本解释器的情况下,可以直接运行脚本(使用相对路径指定)。在此之前,需查看脚本的权限,若无执行权限,则需修改(chmod 764 filename.sh
    (base) May5 17:08:51 ~
    $ vim test.sh
    (base) May5 17:17:20 ~
    $ ll test.sh 
    -rw-rw-r-- 1 May5 May5 31 May 25 17:17 test.sh
    (base) May5 17:20:20 ~
    $ chmod 764 test.sh 
    (base) May5 17:20:40 ~
    $ ll test.sh 
    -rwxrw-r-- 1 May5 May5 31 May 25 17:17 test.sh*
    (base) May5 17:20:45 ~
    $ ./test.sh   #设定test.sh的头命令(指定脚本解释器),则不需要bash 命令执行脚本
    hello world
    

    vim编辑器与R脚本

    • test.R
    a=1:8
    a
    
    • Rscript test.R运行R脚本
    (base) May5 17:51:59 ~
    $ cat test.R
    a=1:8
    a
    
    (base) May5 17:52:33 ~
    $ Rscript test.R
    [1] 1 2 3 4 5 6 7 8
    
    • 设定Rscript解释器的头命令
    (base) May5 17:50:27 ~
    $ which Rscript   #查看Rscript的目录
    /usr/bin/Rscript
    (base) May5 17:55:07 ~
    $ vim test.R   #通过vim编辑器指定Rscript的头命令
    (base) May5 17:57:07 ~   
    $ cat test.R  #查看test.R的脚本内容
    #!/usr/bin/Rscript  #Rscript的头命令
    a=1:8
    a
    (base) May5 17:56:54 ~
    $ ~/test.R    #直接运行脚本(通过相对路径)
    [1] 1 2 3 4 5 6 7 8
    

    标准输出--1;及标准误输出--2

    • 标准输出--1:命令输出正确时的重定向
    (base) May5 17:59:45 ~
    $ ls
    Data        file1.txt  file3.txt  file5.txt  file7.txt  file9.txt   teach   test.R~
    file10.txt  file2.txt  file4.txt  file6.txt  file8.txt  miniconda3  test.R  test.sh~
    (base) May5 18:20:59 ~
    $ ls 1>out1.txt
    (base) May5 18:21:13 ~
    $ cat out1.txt 
    Data
    file10.txt
    file1.txt
    file2.txt
    file3.txt
    file4.txt
    file5.txt
    file6.txt
    file7.txt
    file8.txt
    file9.txt
    miniconda3
    out1.txt
    teach
    test.R
    test.R~
    test.sh~
    
    • 标准误输出--2: 命令输出错误时的重定向
    (base) May5 18:21:35 ~
    $ lsddff   ##输入错误的命令
    No command 'lsddff' found, did you mean:   ##输出错误的信息
     Command 'lsdiff' from package 'patchutils' (main)
    lsddff: command not found
    (base) May5 18:23:07 ~
    $ lsddf 1>out2.txt   ##使用标准输出后仍有错误信息
    lsddf: command not found
    (base) May5 18:23:18 ~
    $ lsddff 2>out2.txt   ##使用标准误输出重定向后命令执行
    (base) May5 18:23:37 ~
    $ cat out2.txt   #查看标准误输出后重定向的文件可返回之前的错误信息
    No command 'lsddff' found, did you mean:
     Command 'lsdiff' from package 'patchutils' (main)
    lsddff: command not found
    

    任务提交后台(服务器)

    任务提交后台
    • [command]+& 将...命令挂在服务器,但不稳定,断电后即中断
    • nohup [command] 将...命令挂在服务器,断电后即不会中断

    &与nohup的用法: https://zhuanlan.zhihu.com/p/59297350

    实际上,这种需求在现实中很常见,比如想远程到服务器编译程序,但网络不稳定,一旦掉线就编译就中止,就需要重新开始编译,很浪费时间。
    在这种情况下,我们就可以使用nohup命令。nohup就是不挂起的意思( no hang up)。该命令的一般形式为:

    nohup ./test &
    

    如果仅仅如此使用nohup命令的话,程序的输出会默认重定向到一个nohup.out文件下。如果我们想要输出到指定文件,可另外指定输出文件:

    nohup ./test > myout.txt 2>&1 &
    

    这样一来,多管齐下,既使用了nohup命令,也使用了&符号,同时把标准输出/错误重定向到指定目录下。

    另一种挂后台的方法:setsid 命令https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/

    nohup 无疑能通过忽略 HUP 信号来使我们的进程避免中途被中断,但如果我们换个角度思考,如果我们的进程不属于接受 HUP 信号的终端的子进程,那么自然也就不会受到 HUP 信号的影响了。setsid 就能帮助我们做到这一点。
    值得注意的是,上例中我们的进程 ID(PID)为31094,而它的父 ID(PPID)为1(即为 init 进程 ID),并不是当前终端的进程 ID。

    具体示例:

    setsid bash download.sh
    

    相关文章

      网友评论

          本文标题:linux学习笔记---5:vim编辑器及命令执行,头命令;标准

          本文链接:https://www.haomeiwen.com/subject/jyxuahtx.html