Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
These are my files:
$ ls -al my_fake_service.*
-rwxr--r-- 1 user1 user1 396 Mar 20 16:33 my_fake_service.sh
-rw-rw-r-- 1 user1 user1 25 Mar 20 16:17 my_fake_service.txt
# Is this how to include a file into Bash script?
./home/user1/test/linuxlab/my_fake_service.txt
if [[ ${VAR1} == "true" ]]
then
echo "VAR1 set to true" >> ~/test/linuxlab/my_fake_service.log
else
echo "VAR1 set to false" >> ~/test/linuxlab/my_fake_service.log
fi
if [[ ${VAR2} == "true" ]]
then
echo "VAR2 set to true" >> ~/test/linuxlab/my_fake_service.log
else
echo "VAR2 set to false" >> ~/test/linuxlab/my_fake_service.log
fi
--------------------------- When I ran the script, I got this error. What is causing it?
~/test/linuxlab $ bash my_fake_service.sh
my_fake_service.sh: line 2: ./home/foffb/test/linuxlab/my_fake_service: No such file or directory
user1> ~/test/linuxlab $ ls -al my_fake_service.*
-rw-r--r-- 1 user1 user1 36 Mar 20 17:13 my_fake_service.log
-rwxr--r-- 1 user1 user1 396 Mar 20 16:33 my_fake_service.sh
-rw-rw-r-- 1 user1 user1 25 Mar 20 16:17 my_fake_service.txt
And here is the incorrect results:
user1> ~/test/linuxlab $ cat my_fake_service.log
VAR1 set to false
VAR2 set to false
After 46 posts, if you cannot remember to use [code][/code] tags around your code / data, then I shall leave it to others to try and read your question.
If you were talking about grail's comment, it was a very valid one. It is far easier to understand code inserted in the relevant CODE tags. It is also accepted forum etiquette.
I have some advice for you. The answers to the questions you've asked this evening, including this one, could be solved with a minimum of Googling plus, more importantly, the reading of one of many guides on how to script in Bash. You would find it far easier to proceed if you find one of these and just spend some time learning the fundamentals. Just take some time to do that and you'll find your scripting experience to be much easier.
Thank you for trying ...
It was sample code that is suppose to work.
That is why I was asking.... since I could not find a solution online yet at the time..... !
So the correct thing I believe to do here is to source the text file. A test which you can perform, as I always also do, is to follow the regimen that "everything you can do within the bash shell prompt, you can do within a script".
Explanation: I "sourced" 1.txt which contained a simple variable assignment. I validated in advance that the variable was not present in my bash shell environment. After sourcing that text file, the variable was now present in my shell environment.
Therefore I suggest you source your text file.
Be aware that other things besides variable assignments may not work. You'll have to test them out. But since this is exactly what you are doing, variable assignments, I believe it may work for you. Give it a try.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.