操作系统自用2

本文介绍了LinuxBash脚本的基础知识,包括基本命令如cd,ls,date等,以及脚本文件的创建与执行。讨论了shebang的作用,如何赋予脚本执行权限,并展示了Bash中的算术运算和控制结构,如if条件语句和循环。此外,还提到了文件权限的chmod设置。
摘要由CSDN通过智能技术生成

OS Programme Lecture #2 Linux Bash Scripting Tutorial

1. Bash basics

$cd

$pwd

$date

$ls

$ls -l

$ls | sort

$ls -l > filelist.txt

$ls -l >> filelist.txt

$ls -l | wc -w

$ls filelist.txt | wc -w

$ls Documents

$ls Documents | wc -w

$cat

$cp

$rm

$mv

$wc //word count

$echo hello world!

$echo *

$echo **

$echo ***

$find *.sh

$seq 1 10

$seq 1 2 10

2. Bash 执行脚本文件

创建一个bash脚本文件:

$touch hello_world.sh

$vim hello_script.sh (进入编辑,按insert键编辑脚本,完成后shift+;退出,若要保存输入wq!回到控制台,若不保存,输入q!)

shebang,它告诉shell在执行脚本时使用哪个程序来解释脚本。

#!/bin/bash — Execute the file using bash

#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell

#!/bin/csh — Execute the file using csh, the C shell, or a compatible shell

#!/usr/bin/perl -T — Execute using Perl with the option for taint checks

#!/usr/bin/php — Execute the file using the PHP command line interpreter

#!/usr/bin/python -O — Execute using Python with optimizations to code

#!/usr/bin/ruby — Execute using Ruby

执行bash脚本,有以下方式:

第一种

$bash hello_world.sh

$sh hello_world.sh

第二种:设置可执行权限,使用chmod +x或者chmod u+x

u stands for user.

g stands for group.

o stands for others.

a stands for all.

x stands for executable.

$chmod u+x hello_world2.sh

$./hello_world2.sh

3. Bash arithmetic

$expr whatever

$expr 1+2

$expr 1 + 2

$expr 1 - 2

$expr 1 * 2

$expr 1 "*" 2

$expr 2 "*" 2

$let x=666

$let y=334

$let z=$x+$y

$$z

$echo $z

$let w=$x/6

$echo $w

$let count=2

$echo $count

$count=$count+1

$echo $count

$a=10

$echo $a

$b=YES

$echo $b

4. Bash control structures

看脚本文件 if.sh, case.sh, while.sh, until.sh, for.sh, arguments.sh, functions.sh

-eq equal to

-ne not equal to

-lt less than

-le less or equal to

-gt greater than

-ge greater than or equal to

$chmod 755 if.sh

755 means full permissions.

注意:在脚本里,$# 代表 number of arguments passing to the script,$* 代表 all positional arguments.

版权归广大操作系统实验课老师所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值