我正在尝试制作一个脚本 B,它使用另一个脚本 A 来更改特定行中的特定列。然而,当我单独运行脚本 A 时,它运行得很好。但是当我尝试使用脚本 B(允许我在其他目录中运行脚本 A)时,它会更改整行而不是特定列。
问题是什么?
脚本A(0.MOF.run
):
#!bin/sh
echo $pot
mkdir u$pot
cp u6000/towhee_input u$pot
cd u$pot
mv towhee_input 1
awk < 1 -v k="-$pot.0" 'NR==12 {$2=k}{print}' > towhee_input
cd ..
脚本B:
#!bin/sh
echo -n "Please Incert the Number of Potencials: "
read c
declare -i b
b=$c+1
rm pot_pid
a=1
while [ $a -lt $b ]
do
pot=$(awk < data -v k="$a" 'NR==k ')
. 0.MOF.mkdir
. 1.MOF.run
a=`expr $a + 1`
done
答案1
正如我在评论中所说,我无法重现您的问题。如果您向我们提供测试所需的数据,我将更新此数据,但与此同时,这里有一种更简单的方法来编写您到目前为止所拥有的内容:
#!/usr/bin/env bash
## Write a function instead of calling
## an external script. You could also make
## 0.MOF.mkdir a function.
MOF_run() {
echo "a.sh pot is $pot"
## The -p will cause mkdir to fail
## silently if the directory exists.
mkdir -p u$pot
## Run the awk on the target file, no need to cd or copy
awk < u6000/towhee_input -v k="-$pot.0" 'NR==12 {$2=k}{print}' > u$pot/towhee_input
}
echo -n "Please Insert the Number of Potentials: "
read c
## Make sure the user entered a number
## and not anything else or an empty string.
while [[ -z $c || ! $c =~ ^[0-9]*$ ]]; do
echo "Please enter a number."
echo -n "Please Insert the Number of Potentials: "
read c
done;
## increase c by one
let c++
rm pot_pid
a=1
while [ $a -lt $c ]
do
pot=$(awk < data -v k="$a" 'NR==k ')
echo "b.sh pot is $pot";
. 0.MOF.mkdir
## Call the MOF_run function
MOF_run
## increment $a
let a++;
done