lnlidawei

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

[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、  无

 

  .

posted on 2026-02-21 22:20  lnlidawei  阅读(4)  评论(0)    收藏  举报