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脚本编程

目录
相关文章
|
14天前
|
Shell Linux
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
77 32
|
14天前
|
存储 Linux
linux中的目录操作函数
本文详细介绍了Linux系统编程中常用的目录操作函数,包括创建目录、删除目录、读取目录内容、遍历目录树以及获取和修改目录属性。这些函数是进行文件系统操作的基础,通过示例代码展示了其具体用法。希望本文能帮助您更好地理解和应用这些目录操作函数,提高系统编程的效率和能力。
64 26
|
4月前
|
存储 Shell
Shell 数组
【10月更文挑战第16天】
62 3
|
4月前
|
Ubuntu Linux Python
Tkinter错误笔记(一):tkinter.Button在linux下出现乱码
在Linux系统中,使用Tkinter库时可能会遇到中文显示乱码的问题,这通常是由于字体支持问题导致的,可以通过更换支持中文的字体来解决。
263 0
Tkinter错误笔记(一):tkinter.Button在linux下出现乱码
|
5月前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
2月前
|
Linux
【Linux】System V信号量详解以及semget()、semctl()和semop()函数讲解
System V信号量的概念及其在Linux中的使用,包括 `semget()`、`semctl()`和 `semop()`函数的具体使用方法。通过实际代码示例,演示了如何创建、初始化和使用信号量进行进程间同步。掌握这些知识,可以有效解决多进程编程中的同步问题,提高程序的可靠性和稳定性。
106 19
|
2月前
|
Linux Android开发 开发者
linux m、mm、mmm函数和make的区别
通过理解和合理使用这些命令,可以更高效地进行项目构建和管理,特别是在复杂的 Android 开发环境中。
87 18
|
2月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
126 13
|
3月前
|
存储 Shell Linux
Linux 如何更改默认 Shell
Linux 如何更改默认 Shell
108 0
Linux 如何更改默认 Shell
|
4月前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
113 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】