[bash]:BASH中的变量——可先用后定义
[bash]:BASH中的变量——可先用后定义
一、说明
1、 BASH版本信息
[wit@test:tmp]$ bash --version
GNU bash, version 5.3.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2025 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[wit@test:tmp]$
[wit@test:tmp]$
二、脚本
[wit@test:tmp]$ cat bash_test
#!/usr/bin/env bash
function do_msg()
{
echo
ls -lh .
echo
echo -e "\t[DO_MSG]#\tHahaha......"
# variables(no position): first use , last define.
echo -e "\t[DO_MSG]#\t${FILE_NAME}"
}
# function: if FILE exists then DO_SOMETHING.
# function: if $1($1==FILE) exists; then $2(DO_SOMETHING)
function test_file_do()
{
if [[ -e $1 ]]
then
$2
fi
}
FILE_NAME="books.txt"
touch ${FILE_NAME}
# if ${FILE_NAME} exists; then do_msg
test_file_do ${FILE_NAME} do_msg
[wit@test:tmp]$
三、参考内容
1、 无
.
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章版权归属【原创作者】; 转载或引用时【请保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/19628277

浙公网安备 33010602011771号