我只是在想,有没有什么办法可以先获取一个文件,然后再跟踪该文件的来源?
我在我的bash脚本上使用,我在主脚本上使用easyoption.sh,它工作得很好。但是,当我有其他脚本稍后从主脚本加载时,我希望获得帮助,并且-- easyoptions.sh应该在最后加载的文件上工作。
示例:
test.sh
#!/bin/bash
## EasyOptions Sub Test
## Copyright (C) Someone
## Licensed under XYZ
## -h, --help All client scripts have this, it
所以我有一个bash脚本调用另一个bash脚本。第二个脚本在不同的文件夹中。
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
在第二个脚本中,它有行src=$(pwd),因为我是从不同目录中的另一个脚本调用该脚本,所以$(pwd)返回第一个脚本的当前目录。
有没有办法在第二个脚本中使用一个简单的命令来获取该脚本的当前目录,而不必传递参数
我从脚本A调用bash脚本B。在脚本A(父脚本)中,我导出了一些变量。我想在脚本B (下标)中使用这些变量,但是变量值没有从脚本A传递到脚本B。有没有办法从脚本B访问变量值?
#!/bin/bash
# script_A.sh
export VAR="value"
enter code here
sudo -u user ./script_B.sh
#!/bin/bash
# script_B.sh
echo $VAR # this prints nothing
我正在我的bash脚本(比如child.sh)中调用bash脚本(比如parent.sh),我希望脚本(child.sh)中的错误被困在我的父脚本(parent.sh)中。
我通过和阅读。基于此,我认为我应该对我的父脚本执行set -E,以便由子shell继承这些陷阱。因此,我的代码如下
parent.sh
#!/bin/bash
set -E
error() {
echo -e "$0: \e[0;33mERROR: The Zero Touch Provisioning script failed while running the command $BASH_COMM
我在终端中得到一组字符串作为输入。我需要使用".awk" shell将每个字符串中的子字符串替换为".sh",然后输出修改后的字符串。我写了这样的脚本来做这个:
#!/bin/bash
while read line
do
result=${line/.awk/.sh}
echo $result
done
但是它给了我一个错误:script-ch.sh: 6: script-ch.sh: Bad substitution。
如何更改这个简单的脚本以修复错误?
UPD: ".awk"可能在字符串中。例如:"file.awk.old
我没有给出真实的例子,因为这篇文章太长了,以至于没有人会读它。所以我会给出其他例子,我有一个bash脚本name.sh,它被编程如下-
#!/bin/bash
echo "Please Enter your name"
read name
echo "Hello $name"
sudo bash age.sh
另一个脚本(比如age.sh)是这样的-
#!/bin/bash
*echo "Hello $name"
echo "Please enter your age"
read age
echo "Thank you
关于使用pgrep搜索哪些shell进程正在运行与当前脚本相同的脚本,这里有一个非常奇怪的问题。
下面是名为test.sh的测试脚本
#!/bin/bash
full_res=`pgrep -a -l -f 'test\.sh'`
res=$(pgrep -a -l -f 'test\.sh' | cat)
echo "short result is $full_res"
echo "weird result is $res"
有输出
sh test.sh &
[1] 19992
➜ logs short res
我在同一个目录下有3个脚本,请查看下面x.sh,y.sh和z.sh的内容:
x.sh :-
xData="DataOfX"
function xInit(){
echo "xInit : data of a >$xData<"
}
y.sh :-
. x.sh
xInit
sh z.sh zInit
z.sh :-
function zInit(){
echo "zInit of z"
xInit
}
$@
正在执行中
。y.sh
在同一目录中给出以下输出:-
xInit : data of a &g