Alexandre34
Posts: 21
Joined: Sun Jan 31, 2016 6:18 am

script for restore missing file

Tue Apr 03, 2018 12:12 pm

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 ?

User avatar
topguy
Posts: 6466
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: script for restore missing file

Tue Apr 03, 2018 12:57 pm

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

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: script for restore missing file

Tue Apr 03, 2018 4:12 pm

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.

Return to “Beginners”