9、Linux Shell 笔记(2),函数和数组

简介: 1、重定向很多数据,用echo就不方便了。我们用exec命令。 如exec 1>testout Shell 中最多可以有9个打开的文件描述符。 2、重定向 exec 3>&1 //脚本将文件描述符3重定向到文件 exec 3testfile //exec使命令将文件描述符3分配给文件testfile的输入输出操作 重定向到特殊符号&-来关闭文件描述符。

1、重定向很多数据,用echo就不方便了。我们用exec命令。

exec 1>testout

Shell 中最多可以有9个打开的文件描述符。

2、重定向

exec 3>&1 //脚本将文件描述符3重定向到文件

exec 3<>testfile //exec使命令将文件描述符3分配给文件testfile的输入输出操作

重定向到特殊符号&-来关闭文件描述符。

列向所有文件描述符:lsof

/dev/null

将输出同时发送到监视器和日志文件,用tee命令。

Mktemp 来创建临时文件和昨时目录。

在同一步物理介质的文件间只能创建一个硬链接。

3、函数

Function name

{

commands

}

另外一种格式:

Name(){

commands

}

函数的退出状态是函数的最后一条命令返回的退出状态。Return

函数输出

Function db

{

Read -p "Enter a value: " value

Echo $[$value * 2]

}

Result = 'db'

Echo "It's $Result"

Shell将函数作为小型脚本处理,可以像普通脚本那样给其传递参数。

默认情况下,脚本中定义的变量都是全局变量。

局部变量:local temp

数组变量和函数

Passing arrays to functions

The art of passing an array variable to a script function can be confusing. If you try to pass the array variable as a single parameter, it wont work:

$ cat badtest3

#!/bin/bash

# trying to pass an array variable

function testit {

echo "The parameters are: $@"

thisarray=$1

echo "The received array is ${thisarray[*]}"

}

myarray=(1 2 3 4 5)

echo "The original array is: ${myarray[*]}"

testit $myarray

$ ./badtest3

The original array is: 1 2 3 4 5

The parameters are: 1

./badtest3: thisarray[*]: bad array subscript

The received array is

$

If you try using the array variable as a function parameter, the function only picks up the first value of the array variable.

To solve this problem, you must disassemble the array variable into its individual values, then use the values as function parameters. Inside the function, you can reassemble all of the parameters into a new array variable. Heres an example of doing this:

$ cat test10

#!/bin/bash

# array variable to function test

function testit {

local newarray

newarray=(`echo "$@"`)

echo "The new array value is: ${newarray[*]}"

}

myarray=(1 2 3 4 5)

echo "The original array is ${myarray[*]}"

testit ${myarray[*]}

$ ./test10

The original array is 1 2 3 4 5

The new array value is: 1 2 3 4 5

$

The script uses the $myarray variable to hold all of the individual array values to place them all on the command line for the function. The function then rebuilds the array variable from the command line parameters. Once inside the function, the array can be used just like any other array:

$ cat test11

#!/bin/bash

# adding values in an array

function addarray {

local sum=0

local newarray

newarray=(`echo "$@"`)

for value in ${newarray[*]}

do

sum=$[ $sum + $value ]

done

echo $sum

}

myarray=(1 2 3 4 5)

echo "The original array is: ${myarray[*]}"

arg1=`echo ${myarray[*]}`

result=`addarray $arg1`

echo "The result is $result"

$ ./test11

The original array is: 1 2 3 4 5

The result is 15

$

The addarray function iterates through the array values, adding them together. You can put any number of values in the myarray array variable, and the addarray function will add them.

Returning arrays from functions

Passing an array variable from a function back to the shell script uses a similar technique. The function uses an echo statement to output the individual array values in the proper order, thenthe script must reassemble them into a new array variable:

$ cat test12

#!/bin/bash

# returning an array value

function arraydblr {

local origarray

local newarray

local elements

local i

origarray=(`echo "$@"`)

newarray=(`echo "$@"`)

elements=$[ $# - 1 ]

for (( i = 0; i <= $elements; i++ ))

{

newarray[$i]=$[ ${origarray[$i]} * 2 ]

}

echo ${newarray[*]}

}

myarray=(1 2 3 4 5)

echo "The original array is: ${myarray[*]}"

arg1=`echo ${myarray[*]}`

result=(`arraydblr $arg1`)

echo "The new array is: ${result[*]}"

$ ./test12

The original array is: 1 2 3 4 5

The new array is: 2 4 6 8 10

The script passes the array value, using the $arg1 variable to the arraydblr function. The arraydblr function reassembles the array into a new array variable, and it makes a copy for the output array variable. It then iterates through the individual array variable values, doubles each value, and places it into the copy of the array variable in the function.

The arraydblr function then uses the echo statement to output the individual values of the array variable values. The script uses the output of the arraydblr function to reassemble a new array variable with the values.

参考:

1Linux命令行和SHELL脚本编程

目录
相关文章
|
9月前
|
算法 Linux Shell
Linux实用技能:打包压缩、热键、Shell与权限管理
本文详解Linux打包压缩技巧、常用命令与原理,涵盖.zip与.tgz格式操作、跨系统传文件方法、Shell运行机制及权限管理,助你高效使用Linux系统。
Linux实用技能:打包压缩、热键、Shell与权限管理
|
9月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
817 9
|
9月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
847 2
|
11月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1231 1
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
2102 25
|
Linux Shell
shell_42:Linux参数移动
总的来说,参数移动是Linux shell脚本中的一个重要概念,掌握它可以帮助我们更好地处理和管理脚本中的参数。希望这个解释能帮助你理解和使用参数移动。
327 18
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
391 4
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
653 3
|
运维 监控 中间件
Linux运维笔记 - 如何使用WGCLOUD监控交换机的流量
WGCLOUD是一款开源免费的通用主机监控工具,安装使用都非常简单,它可以监控主机、服务器的cpu、内存、磁盘、流量等数据,也可以监控数据库、中间件、网络设备
|
Shell Linux
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
1471 32