Page 1 of 1

script for restore missing file

Posted: Tue Apr 03, 2018 12:12 pm
by Alexandre34
Hi everybody.
I am not very skilled with shell scripts, so I prefer to ask: I want to copy the file /boot/config.old to /boot/config.txt IF /boot/config.txt is missing OR has a size=0
I can make it with a c program, but I would prefer make a shell script or use commands in the /etc/rc.local script.
Do you have a example of testing if file exists or size=0 in a script ?

Re: script for restore missing file

Posted: Tue Apr 03, 2018 12:57 pm
by topguy
Since we assume its a text file you can use "wc" to count text lines. A count of zero means empty file or at least not a proper config.txt

Code: Select all

wc -l  < /boot/config.txt
or you can count bytes.

Code: Select all

wc -c  < /boot/config.txt

Re: script for restore missing file

Posted: Tue Apr 03, 2018 4:12 pm
by B.Goode
The default shell for the pi user under Raspbian is bash.

So you need to refer to the documentation for bash

Code: Select all

man bash
Scroll a long way down to Conditional Expressions.

You will find
-a file
True if file exists

and
-s file
True if file exists and has a size greater than zero.