bash初学

这篇博客详细介绍了bash脚本的基础知识,包括创建脚本、输出重定向、条件语句、循环、输入输出、管道操作、字符串和运算。还涵盖了declare命令、数组、函数以及文件的间接访问等内容,同时讲解了如何用bash脚本发送邮件、监控文件以及使用grep、awk和sed指令进行文本处理。此外,还涉及了shell脚本的调试技巧。
摘要由CSDN通过智能技术生成

目录

创建脚本

hello world

redirect to file

comment

conditional statements

loop(循环)

input

output

pipes

字符串

运算

declare 命令

数组

函数

文件的间接访问

用脚本发邮件

select case

-t -n -s

监视文件

grep指令

awk指令

sed指令

调试


 

创建脚本

cat /etc/shells
cd 桌面
touch helloscript.sh

hello world

#! /bin/bash

echo "Hello World"

$ ls -al #查看权限
$ chmod +x helloscript.sh 
$ ls -al #文件已变成可执行文件
$ ./helloscript.sh

redirect to file

#! /bin/bash

echo "Hello World" > file.txt

生成file.txt文件,打开看到hello world

cat > file.txt

$ ./hello.sh
# 输入文字 如 hello this is shell ctrl+d
# 打开file.txt

cat >> file.txt
#在后面添加,不是替换

cat 是一个文本文件查看和连接工具。查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名,如cat linuxyw.txt

< 输入重定向

> 输出重定向

>> 输出重定向,进行追加,不会覆盖之前内容

<< 标准输入来自命令行的一对分隔号的中间内容

cat << eof
this is a test
for cat eof
eof

在一个文件中写入内容

cat > /etc/docker/daemon.sh <<EOF
{
  hello hi how are you
  i am fine
  thank you
}
EOF

EOF只是个标识,不是固定的

comment

# 单行注释
: '
多行注释
多行注释
多行注释
多行注释
多行注释
多行注释'

conditional statements

常用逻辑运算符

-f	 	判断文件是否存在 eg: if [ -f filename ];
-d	 	判断目录是否存在 eg: if [ -d dir     ];
-eq		等于,应用于整型比较 equal;
-ne		不等于,应用于整型比较 not equal;
-lt		小于,应用于整型比较 letter;
-gt		大于,应用于整型比较 greater;
-le		小于或等于,应用于整型
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值