0

I am trying to make a script B that uses another script A to change a specific column in a specific line. However, when I run the script A alone it runs perfectly. But when I try to use the script B (allowing me to run script A in other directories) it changes the entire line and not the specific column.

What is the problem?

script 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 ..

script 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
  • i my sorry but i cant't edit properly the question.. i dont know how to do it – Joao Macau May 19 '14 at 12:00
  • Can you be more precise? e.g In the 2nd script you call 1.MOF.run but you said that your script is named 0.MOF.run. Moreover to call a script that is executable (chmod +x myscript) you should write ./myscript or /bin/bash myscript ... – Hastur May 19 '14 at 12:41
  • the 1.MOF.run, it's other script used to run simulations. The 0.MOF.mkdir, is a script to crated directories and change some parameters. The parameter that i wont to exchange is in the 2 column, and when i run the 0.MOF.mkdir alone it works perfectly.. but when i run it in the script B it change the entire line, not the specific column – Joao Macau May 19 '14 at 12:57
  • 1
    I can't reproduce this, only the 12th line is changed in my tests. Please provide us with a minimal sample of input data that reproduces the problem for you. This is either somehow specific to your data or depends on the contents of . 0.MOF.mkdir. My guess is that whatever you are using to create towhee_input is not using the right field separators. – terdon May 19 '14 at 13:37
  • I see a problem of scope: check if it is true. In the scriptB it is given a values to the variable pot. Then there is the try to use it in the scriptA. If you do not pass this value somehow (./scriptA $pot in scriptB, then pot=$1 in scriptA), the scriptA will not know the value of pot and it cannot act correctly in the awk call. As a side effect you should create a directory u when you execute your script. – Hastur May 19 '14 at 18:57
1

As I said in my comment, I can't reproduce your issue. If you give us the data necessary to test, I will update this but in the meantime, here's a simpler way of writing what you have so far:

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.