LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2018, 05:07 PM   #1
TheNun
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Rep: Reputation: Disabled
Bash code cannot read file content after PHP writes into it!


So I have on my raspberry pi an website. On this website I have an file "PiConfig.php".

In this file "PiConfig.php" I have this code written:

Code:
$myfile = fopen("/var/www/html/AdminPanel/State.txt", "w") or die("Unable to open file2!");
$txt = 1;
fwrite($myfile, $txt);
fclose($myfile);
On my raspbery pi I have configured an cronjob, which executes every minute this script "test.sh"

In this file "test.sh" I habe this code written:

Code:
#!/bin/bash

State="none2"

input="/var/www/html/AdminPanel/State.txt"
while IFS= read -r var
do
State="$var"
done < "$input"

echo "$State"
MY PROBLEM:

When I create this file "/var/www/html/AdminPanel/State.txt" on my linux machine by a commande (for example with "touch") and I try to execute my script "test.sh" then it echos "1" which I want! But if I call my "PiConfig.php" on my webbrowser and it writes the value "1" inside the "/var/www/html/AdminPanel/State.txt" file, which is still fine. But the problem now is, when I execute my "test.sh" it doesn't echo "1" but "none2" which means it doesn't have read the content out of the file.. Always when PHP writes into the file, my bash code can't read out of it. I tryed everything to change with the user and group "rights" but nothing works. Pleaseeee help <3

These are permissions before the PHP script writes into the textfile.
-rw-r--rw- 1 pi pi State.txt

These are the permissions after I execute the PHP script "PiConfig.php".
-rw-r--rw- 1 pi pi State.txt

The permissions do not change.

When I now edit the textfile "State.txt" with the command "nano" and then I try to run my bash script "test.sh" the script runs like it should and it echos "1".
When I now try to run the PHP file on my webbrowser "PiConfig.php" and then I try ro run my bash script "test.sh" the script does not run like it should because it echos "none2". (Which means the bash script could't read out the file "State.txt" after the PHP script wrote into it.

Last edited by TheNun; 09-23-2018 at 06:16 PM.
 
Old 09-23-2018, 05:41 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Your trouble almost certainly results from ownership or permission issues. PHP is most likely writing to the file as the web server user, and your bash script is most likely being run as your normal user - although you have not shown us the actual file permissions or told us how you are running the bash script.

So to get useful replies, please show us what the file permissions and ownership are immediately before and after each access, and tell us exactly how you are executing the bash scrpt. It would also be useful to show us what you have tried that did not work, 'I tryed everything to change with the user and group "rights" but nothing works.', does not provide us with any useful information.

Good luck!
 
Old 09-23-2018, 10:58 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Your cron-script should be debugged, details here: https://www.linuxquestions.org/quest...0/#post5707383
 
Old 09-24-2018, 08:33 AM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try checking your "State.txt" files to see if they have a line feed character.
Your read command only works if the line being read has a line feed.
Code:
echo -n "1" > "State.txt"  # Create a file without a line feed
State="none2"
while IFS= read -r var; do
  State="$var"
done < "State.txt"
echo "$State"

none2                   # read did not work


echo "1" > "State.txt"     # Create a file with a line feed
State="none2"
while IFS= read -r var; do
  State="$var"
done < "State.txt"
echo "$State"

1                       # read worked
 
3 members found this post helpful.
Old 09-24-2018, 12:05 PM   #5
TheNun
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thumbs up PERFECT!

Quote:
Originally Posted by Kenhelm View Post
Try checking your "State.txt" files to see if they have a line feed character.
Your read command only works if the line being read has a line feed.
Code:
echo -n "1" > "State.txt"  # Create a file without a line feed
State="none2"
while IFS= read -r var; do
  State="$var"
done < "State.txt"
echo "$State"

none2                   # read did not work


echo "1" > "State.txt"     # Create a file with a line feed
State="none2"
while IFS= read -r var; do
  State="$var"
done < "State.txt"
echo "$State"

1                       # read worked
I got it, my problem is now solved. Thank you so much!
 
Old 09-25-2018, 02:37 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
How to make bash read an incomplete file

Most read implementations, when meeting an incomplete line, give non-zero exit (so while exits) but fill the variable(s) nevertheless.
The following is a general solution how to read a file with bash that may miss the ending newline!
Code:
#!/bin/bash
while IFS= read -r var || [ -n "$var" ]
do
  State=$var
done < "State.txt"
echo "$State"
In case read has non-zero exit it tests the just filled variable. If it has contents, the test has zero exit and the loop runs one more time.

Last edited by MadeInGermany; 09-25-2018 at 09:08 AM. Reason: More general solution and set a subtitle.
 
  


Reply



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
(apache server ) phtml writes all the php code as text in the browser anis123 Linux - Server 12 10-05-2016 12:13 AM
How to create a code that reads and writes my IP into a zone file NS-ME Linux - Server 3 10-17-2011 03:06 PM
[SOLVED] Apache/Php code to recursively display folder content lpallard Programming 1 12-30-2010 04:03 PM
[bash] Read file's content into variable problem Coolmax Programming 6 08-31-2009 04:12 PM
How to read the content of a file with conky Adso Linux - Newbie 5 05-17-2009 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 PM.

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
Open Source Consulting | Domain Registration