bash脚本

author author     2022-08-18     679

关键词:

{**Bash脚本基础**}

BASH=GNU Bourne-Again Shell,BASH是GNU组织开发和推广的一个项目。

Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操作,并可以完成图形工具无法实现的功能。

[1.如何创建新shell脚本]

1.创建包含bash命令的文本文件(一般文件名后加.sh),文件第一行:

 #!/bin/bash

2.使文件可执行(chmod +x scripts)

3.将文件放置在用户的$PATH的目录中

   /bin    用于用户的私有程序

   /usr/local/bin   本地开发、系统上的其他人使用的脚本

   /usr/local/sbin  本地开发、由root使用的脚本

运行脚本:sh+文件名或者直接编写文件所在绝对路径

例如:

[[email protected] mnt]# vim 1.sh      编写脚本(以下为内容)

#!/bin/bash

echo hello world

[[email protected] mnt]# chmod +x 1.sh   给可执行权限

[[email protected] mnt]# /mnt/1.sh       运行脚本

hello world

[[email protected] mnt]# sh 1.sh         运行脚本

hello world

 

[[email protected] mnt]# vim 1.sh

#!/usr/bin/env/tcsh -x

cat /mnt/1.sh

[[email protected] mnt]# sh -x 1.sh   (对于/usr/bin/env这种脚本,执行时用sh)

+ cat /mnt/1.sh

#!/usr/bin/env tcsh -x

cat /mnt/1.sh

 

[[email protected] mnt]# echo $2   (特殊字符如$ ! `` # *等需要在前加转义字符才能显示)

 

[[email protected] mnt]# echo $2

$2

[[email protected] mnt]# echo ‘$2‘   (‘‘强化显示)

$2

[[email protected] mnt]# echo "$2"   (""弱化显示)

$2

[[email protected] mnt]# echo "‘‘"

‘‘

[[email protected] mnt]# echo ‘""‘

""

[[email protected] mnt]# echo ‘‘

‘‘

[[email protected] mnt]# echo ****** ******      (显示当前目录的所有内容)

1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private 1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private

[[email protected] mnt]# echo "****** ******"    (要想显示**需要加"")

****** ******

[[email protected] mnt]# echo "****** `date` ******"

****** Tue Dec 13 08:39:41 EST 2016 ******

[[email protected] mnt]# echo ‘****** `date` ******‘

****** `date` ******

 

[[email protected] mnt]# a=1        给a一个值

[[email protected] mnt]# echo $a    输出a的值

1

[[email protected] mnt]# echo $ab

 

[[email protected] mnt]# echo ${a}b  

1b

[[email protected] mnt]# a=`date`

[[email protected] mnt]# echo $a

Tue Dec 13 08:47:17 EST 2016

 

 

 

 


本文出自 “12106768” 博客,请务必保留此出处http://12116768.blog.51cto.com/12106768/1882458

如何在路径有空格的另一个 bash 脚本中运行 bash 脚本?

】如何在路径有空格的另一个bash脚本中运行bash脚本?【英文标题】:Howtorunbashscriptinsideanotherbashscriptwherepathhasspaces?【发布时间】:2021-08-2800:49:44【问题描述】:您好,我正在尝试从另一个bash脚本中调用一个bash脚本。结构如下... 查看详情

bash脚本

{**Bash脚本基础**}BASH=GNUBourne-AgainShell,BASH是GNU组织开发和推广的一个项目。Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操... 查看详情

bash脚本

{**Bash脚本基础**}BASH=GNUBourne-AgainShell,BASH是GNU组织开发和推广的一个项目。Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操... 查看详情

bash脚本编程之在bash脚本中使用选项

[[email protected]~]#vima#!/bin/bash#Name:abc#Description:Createscript#Author:mylinux#Version:0.0.1#Datatime:03/02/1214:42:00#Usage:mkscriptFILENAMEcat>$1 <<EOF#!/bin/bash#Name:`bas 查看详情

如何使用 bash -c 调用 bash 脚本函数 [重复]

】如何使用bash-c调用bash脚本函数[重复]【英文标题】:Howtocallabashscriptfunctionusingbash-c[duplicate]【发布时间】:2021-06-0407:20:55【问题描述】:[更新]请注意,为了满足minimalreproducibleexample,这是本文的简化代码。脚本中会有更多的功... 查看详情

用于运行 php 脚本的 Bash 脚本

】用于运行php脚本的Bash脚本【英文标题】:Bashscripttorunphpscript【发布时间】:2011-07-2706:54:24【问题描述】:我有一个php脚本,我想使用bash脚本运行,所以我可以使用Cron每隔一分钟左右运行一次php脚本。据我所知,我需要创建bas... 查看详情

bash脚本之6git脚本

留坑 查看详情

bash脚本之6git脚本

留坑 查看详情

如何从脚本本身中获取 Bash 脚本的源目录?

】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获取Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情

如何从脚本本身中获取 Bash 脚本的源目录?

】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获得Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情

如何从脚本本身中获取 Bash 脚本的源目录?

】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获取Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情

bash 脚本不能通过 crontab 工作

】bash脚本不能通过crontab工作【英文标题】:bashscriptdoesn\'tworkthroughcrontab【发布时间】:2017-10-2701:59:17【问题描述】:我正在运行一个将文件传输到我的AWS存储桶的bash脚本。如果我通过终端运行bash脚本,它可以正常工作(通过./... 查看详情

bash脚本

bash脚本 一创建shell脚本 1创建包含bash命令的文本文件 #!/bin/bash###写在文件的第一行,说明用什么解释器,来解释以下写的内容### 2chmod +xscripts3 将文件放置在用户的$PATH的目录中~/bin–用于用户的私有程序/... 查看详情

bash脚本

http://pc.zx98.com/PCbc/jbbc/8038.html 查看详情

如何返回到源 bash 脚本?

】如何返回到源bash脚本?【英文标题】:Howdoyoureturntoasourcedbashscript?【发布时间】:2011-04-0916:08:42【问题描述】:我在bash脚本中使用“source”,如下所示:#!/bin/bashsourcesomeneatscriptthatendsprematurely.sh我想退出someneatscriptthatendsprematur... 查看详情

在 bash 脚本中,我有一组参数要传递给另一个 bash 脚本。我该怎么做? [复制]

】在bash脚本中,我有一组参数要传递给另一个bash脚本。我该怎么做?[复制]【英文标题】:Inabashscript,IhaveanarrayofargumentsIwanttopassinordertoanotherbashscript.HowwouldIdothis?[duplicate]【发布时间】:2021-11-0200:13:38【问题描述】:基本上,我... 查看详情

Bash,...检查是不是安装了程序,使用 bash 脚本 [重复]

】Bash,...检查是不是安装了程序,使用bash脚本[重复]【英文标题】:Bash,...checkifaprogramisinstalledornot,usingbashscript[duplicate]Bash,...检查是否安装了程序,使用bash脚本[重复]【发布时间】:2015-06-2706:09:13【问题描述】:我正在尝试创... 查看详情

bash脚本编程

bash脚本编程的结构: bash脚本编程语言: 脚本类语言 解释性语言 过程式编程语言 过程式编程语言的结构: 顺序执行结构:默认 从上到下,自左而右执行所有的语句(命令) 选择执行结构: 当条件满足或不满足时才会执... 查看详情