English 中文(简体)
为什么要把来源/家长/对/对手/对面/面/面/面面/面/面貌区分开来?
原标题:Why is calling source /path/to/my/file different in function than in ~/.bash_profile?
  • 时间:2023-12-15 23:34:42
  •  标签:
  • bash

我现在要记一下我有多少次担任指挥官,因此我创建了一个档案,以储存该票。 当我操作<代码>时,目标在于以新的价值取代档案。 迄今为止,成功。 但后来打电话到<代码>,我能运行。 得出同样的结果,不会再 over过档案。

如果我关闭指挥线并重新启动指挥线,事情就会被罚款。 I m 试图说明为什么在my_Function末尾的“账面_file_path” 在<代码>~/.bash_profile中,我没有这样说。 任何想法?

${HOME}/bash/.bash_count

#!/bin/bash

declare MY_LDAP_MY_FUNCTION_COUNT=0

在我的发言中,我提供了档案。 当我指挥时,我想加一下,把它写到档案中。

${HOME}/bash/.bash_my_functions

my_function () {
    local count_file_path="${HOME}/bash/.bash_count"

    local curr_count="$MY_LDAP_MY_FUNCTION_COUNT"
    echo "curr_count: $curr_count"

    local incremented=$((curr_count+1))

    # Overwrite existing .bash_count file with new value
    printf  #!/bin/bash

declare MY_LDAP_MY_FUNCTION_COUNT=%s
  "$incremented" > "$count_file_path"

    source "$count_file_path"

    cat "$count_file_path" # same result every time unless I quit and restart the command line
}
问题回答

I m not sure how you are invoking your function but it seems likely the environment value is not what you think it is. You could try printing the value of MY_LDAP_MY_FUNCTION_COUNT before you update the file.


另一种做法是使用环境变量,并且应当能够安全地避免多重同时发生的职业(代谢法有改性-高标准竞赛),但是一种比较少的细化。

1. 启动关键价值储存(使用更敏感的名称):

sqlite3 ~/bash/counters.db  
    create table t (k,v);
    insert into t values("MY_LDAP_MY_FUNCTION_COUNT",0);
 

更新:

sqlite3 ~/bash/counters.db  
    update t set v=v+1 where k="MY_LDAP_MY_FUNCTION_COUNT";
 

改为:

sqlite3 ~/bash/counters.db  
    select v from t where k="MY_LDAP_MY_FUNCTION_COUNT";
 

These can be parameterised and assigned to shell functions or scripts.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签