脚本学习

本文详细介绍了Shell脚本的使用,包括利用gcc编译C代码、自动生成脚本信息的快捷方式,以及一系列文本处理工具的用法,如grep、diff、patch、cut、sort、tr、sed和awk。通过实例展示了如何判断数字、检查文件属性、实现大小写不敏感输入以及查询交互式登录用户等实用脚本示例。
摘要由CSDN通过智能技术生成

脚本(Script)是批处理文件的延伸,是一种纯文本保存的程序,一般来说的计算机脚本程序是确定的一系列控制计算机进行运算操作动作的组合,在其中可以实现一定的逻辑分支等。

一、使用gcc在shell中编写C代码:

gcc的执行过程
使用gcc由C语言源代码文件生成可执行文件要经历四个相互关联的步骤∶预处理(也称预编译,Preprocessing)、编译(Compilation)、汇编(Assembly)和连接(Linking)。
命令gcc首先调用cpp进行预处理,在预处理过程中,对源代码文件中的文件包含(include)、预编译语句(如宏定义define等)进行分析。接着调用cc1进行编译,这个阶段根据输入文件生成以.o为后缀的目标文件。
汇编过程是针对汇编语言的步骤,调用as进行工作,一般来讲,.S为后缀的汇编语言源代码文件和汇编、.s为后缀的汇编语言文件经过预编译和汇编之后都生成以.o为后缀的目标文件。当所有的目标文件都生成之后,gcc就调用ld来完成最后的关键性工作,这个阶段就是连接。在连接阶段,所有的目标文件被安排在可执行程序中的恰当的位置,同时,该程序所调用到的库函数也从各自所在的档案库中连到合适的地方

[root@localhost ~]# yum install gcc -y
[root@41 mnt]# vim hello.c
#include<stdio.h>
main()
{
        printf("HELLO,GCC\n");
        return 0;
}

[root@41 mnt]# chmod +x hello.c
[root@41 mnt]# ./a.out    #默认生成文件
HELLO,GCC
[root@41 mnt]# gcc -Wall hello.c -o custom   #编译并链接生成custom文件
hello.c:2:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main()
[root@41 mnt]# ./custom 
HELLO,GCC
二、自动生成脚本信息
1.快捷键生成

在vim配置文件中追加:

[root@localhost ~]# vim /etc/vimrc 
map <F5> ms:call WESTOS()<cr>'s                   #将生成信息的命令映射到F5键
;autocmd BufNewFile *.sh exec ":call WESTOS()"    #实现新建.sh脚本文件时自动生成信息
func WESTOS ()
        call append(0,"###################################")
        call append(1,"#     Author:      Vincent".("        #"))
        call append(2,"#     Verion:             ".("        #"))
        call append(3,"#     Mail:   820027814@qq.com ".("   #"))
        call append(4,"#     Date:               ".("        #"))
        call append(5,"#     Description:        ".("        #"))
        call append(6,"#                         ".("        #"))
        call append(7,"###################################")
        call append(8," ")
        call append(9,"#!/bin/bash")
endfunc

在文本中按快捷键F5后,文本之前自动生成以下信息:

[root@localhost mnt]# vim file.sh
###################################
#     Author:      Vincent        #
#     Verion:                     #
#     Mail:   820027814@qq.com    #
#     Date:                       #
#     Description:                #
#                                 #
###################################

#!/bin/bash
2.新建脚本中自动生成

将map行为该行

                           autocmd BufNewFile *.sh exec ":call WESTOS()"  
三、文本处理工具

1.grep-print lines matching a pattern

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

   -i, --ignore-case
   -n, --line-number
          Prefix each line of output with the 1-based line  number  within
          its input file.
   -r, --recursive
          Read all files  under  each  directory,  recursively,  following
          symbolic  links  only  if they are on the command line.  This is
          equivalent to the -d recurse option.
   -c, --count
          Suppress  normal output; instead print a count of matching lines
          for each input file.  With the -v,  --invert-match  option  (see
          below), count non-matching lines
    -E, --extended-regexp
          Interpret PATTERN as an extended regular  expression  (ERE,  see
          below).  (-E is specified by POSIX.)
    -v, --invert-match
          Invert the sense of matching, to select non-matching lines.  (-v
          is specified by POSIX.)

过滤查找vincent所在行

[root@141 grep]# grep vince
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值