2

I have two file call a_file and b_file. I was trying to write the a_file, so that it can change a variable in b_file.

echo "This is a_file"
echo "Enter x to display msg1 or y to display msg2"
read input

and

echo "This is b_file"
if [ "$var1" = "x" ]; then
  echo "message 1"
else
  echo "message 2"
fi

Can you please tell me how can I manipulate the variable var1 from the a_file?


This is a update on khachik's answer. it was pretty clear. But what If I want to make that b_file run every minute after the first execution of a_file? (crontab) Should I write it as following.

echo "This is a_file"
echo "Enter x to display msg1 or y to display msg2"
read x
* * * * * ./b_file "$x"

and

echo "This is b_file"
if [ "$1" = "x" ]; then
  echo "message 1"
else
  echo "message 2"
fi
1

3 Answers 3

2

The best way is to pass arguments from the command-line:

echo "This is a_file"
echo "Enter x to display msg1 or y to display msg2"
read x
./b_file "$x"

and

echo "This is b_file"
if [ "$1" = "x" ]
  echo "message 1"
else
  echo "message 2"
fi

About modifiying: you can use sed to modify b_file. cat b_file | sed "s/\\\$var/$x/g"

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for your nice detailed explanation. Now I understand how it works. but what about if you want to execute b_file every minute(after executing the a_file only once). At the last line of a_file, should I type in * * * * ./b_file "$x" Please let me know how can I get it to run at every minute. Thanks a lot.
You can use crontab to run a command every minute. But read should read from a file, and echo should be redirected to a file, or it will send an e-mail. May I ask what do you want do to?
I just want to get a input from a user in one file(that first file should only execute once), then depending on the user's input, my second file should run every hour or so. you answered most of my questions. but wasn't sure how to tell it to run the second file after executing a_file only once. Please look at my original question, I have updated it. Thanks a lot for your help.
Thanks a lot for all the detailed information. I finally end up doing what you said in the comment. Thanks.
2

You can use environment variables. In a_file, export the variable var:

export var="Some_value"

In the b_file, you can use it like:

echo $var

However, you need to source the shell script a_file like:

source a_file

I am assuming here you are using shell scripts.

2 Comments

I understood the export var="some_value" part. can you explain where you would put the command source a_file ?
@NewGuy: On the shell prompt, execute the first script as "source a_file". Then, you can execute the second script as "./b_file". Executing the first script as "./a_file" will not export the variable. So, the second script will not see its value.
0

You could use functions and include the file with function definitions in the files that call the functions. The functions can then take arguments. Start with reading this documentation for writing functions in bash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.