[LEDE-DEV] [patch master 05/15] Use cd ... || exit in case cd fails

Alberto Bursi alberto.bursi at outlook.it
Sat Oct 1 13:50:37 PDT 2016



On 10/01/2016 09:43 PM, Jan-Tarek Butt wrote:
> Hi,
>
> On 10/01/16 10:54, Felix Fietkau wrote:
>> On 2016-10-01 09:44, Oswald Buddenhagen wrote:
>>> On Fri, Sep 30, 2016 at 10:02:19PM +0200, Jan-Tarek Butt wrote:
>>>> diff --git a/scripts/get_source_date_epoch.sh b/scripts/get_source_date_epoch.sh
>>>> -[ -n "$TOPDIR" ] && cd "$TOPDIR"
>>>> +[ -n "$TOPDIR" ] && cd "$TOPDIR" || exit
>>>> diff --git a/scripts/getver.sh b/scripts/getver.sh
>>>> -[ -n "$TOPDIR" ] && cd "$TOPDIR"
>>>> +[ -n "$TOPDIR" ] && cd "$TOPDIR" || exit
>>>>   
>>> these two are plain broken, as the script would just exit if TOPDIR is
>>> empty. you need to use braces.
>> I think a better approach would be to write a wrapper for cd, making it
>> easier to read and review.
>>
>> - Felix
> Felix, can you gif an example in the above case?
>
> cheers
> Tarek

I'm not felix (no duh) but I'd use something like this, (for schellcheck 
it is bad practice, feel free to improve it):

safe_cd(){
cd $1
if [ "$?" = "0" ]
then
true
else
exit
fi
}

then you call this instead of cd in the script, like this

[ -n "$TOPDIR" ] && safe_cd "$TOPDIR"

You can also make a "library" script where you keep all such wrapper functions that are included at the start of each script.


-Alberto



More information about the Lede-dev mailing list