LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-20-2016, 07:18 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
How to import a file into bash script?


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

~/test/linuxlab $ cat my_fake_service.txt
VAR1="true"
VAR2="false"

~/test/linuxlab $ cat my_fake_service.sh
#!/sbin/bash

# 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

Last edited by fanoflq; 03-20-2016 at 07:38 PM.
 
Old 03-20-2016, 07:41 PM   #2
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Answer to self:

Quote:
# Is this how to include a file into Bash script?
./home/user1/test/linuxlab/my_fake_service.txt
should be
# Is this how to include a file into Bash script?
source /home/user1/test/linuxlab/my_fake_service.txt

Last edited by fanoflq; 03-20-2016 at 07:43 PM.
 
Old 03-20-2016, 07:41 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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.
 
Old 03-20-2016, 07:45 PM   #4
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.2 MATE
Posts: 3,491
Blog Entries: 2

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
You weren't denied permission to access it, you were denied permission to execute it, which is what you tried to do.
 
Old 03-20-2016, 07:45 PM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
grail:

Quote:
Picky picky picky..... :-)
Oh well.... thanks for reading....

Last edited by fanoflq; 03-20-2016 at 07:48 PM.
 
Old 03-20-2016, 07:47 PM   #6
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
hydrurga:
Quote:
You weren't denied permission to access it, you were denied permission to execute it, which is what you tried to do.
See my answer to self .......

Last edited by fanoflq; 03-20-2016 at 07:48 PM.
 
Old 03-20-2016, 07:52 PM   #7
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.2 MATE
Posts: 3,491
Blog Entries: 2

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
Quote:
Originally Posted by fanoflq View Post
Oh well.... thanks for reading....
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.

Last edited by hydrurga; 03-20-2016 at 07:53 PM.
 
Old 03-20-2016, 08:00 PM   #8
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
hydrurga:

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

Feel better now?... :-)

Last edited by fanoflq; 03-21-2016 at 12:58 PM.
 
Old 03-20-2016, 08:17 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,825
Blog Entries: 12

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
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".

So:
Code:
$ echo $ABC
$
$ echo "ABC=123" > 1.txt
$ cat 1.txt
ABC=123
$ source 1.txt
echo $ABC
123
$
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.

Last edited by rtmistler; 03-20-2016 at 08:20 PM.
 
Old 03-21-2016, 05:28 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You probably missed the extra '.' ie an alternative way to 'source' a file is
Code:
. file
ie dot<space>file
 
1 members found this post helpful.
Old 03-21-2016, 12:57 PM   #11
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
chrism:

Thank you.
Initially, I tried that but without the space.... which is incorrect.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to manually import CSV into sqlite3 -> Spaces mess up everything burnhot Linux - Software 1 02-18-2015 02:17 PM
[SOLVED] bash Script to import data in mysql DB kamran.ayub Linux - Software 3 10-30-2014 11:30 AM
[SOLVED] Bash - Import a script YellowSnowIsBad Programming 5 12-29-2010 03:52 AM
how do you import bash script to a c++ program? jaepi Programming 2 05-02-2007 04:28 AM
grab - Simply a useless bash script to take a screen shot; Required command: import micxz Programming 2 10-08-2003 08:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration