bash - check if file exists, then perform action if not
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash - check if file exists, then perform action if not
Hi all,
I use command below
Code:
if [ -f /mnt/wd/file.txt ]; then echo 'file exists'; else echo 'file does not exist'; fi
and it does work fine except those cases when there is input/output error when reading from usb drive. I tried to catch that i/o error using some construction like
Code:
if ls /mnt/wd/ | grep -i "error" = error ; then echo 'there is i/o error'; else echo 'there is no i/o error'; fi
but need your help to figure out because of my poor knowledge.
Please advise.
there is a stdout and a stderr. stdout is standard out, all the normal messages sent to it, stderr is standard error, all the error messages are sent to here. https://en.wikipedia.org/wiki/Standa...t_.28stdout.29
if you want to grep on stderr you need to redirect it:
ls /mnt/wd 2>&1 | grep -i error
also if you want to check if stderr contains the word error:
if ls /mnt/wd 2>&1 | grep -i error; then ....
do not need == error.
As usual, ls is a poor command to utilise for this type of thing. Why not use the mount command to test if the drive is mounted or not and then continue with file testing if that is the case.
if grep -q '/mnt/wd' /proc/mounts; then
if [ -f /mnt/wd/file.txt ]; then echo 'file exists'; else echo 'file does not exist'; fi
else
echo "Not Checking -- /mnt/wd not mounted."
fi
$ ls /mnt/wd
ls: reading folder /mnt/wd: Input/output error
But when I run
Code:
if grep -q '/mnt/wd' /proc/mounts; then if [ -f /mnt/wd/file.txt ]; then echo 'file exists'; else mount -U 84B46F79B46F6C9C /mnt/wd; fi else mount -U 84B46F79B46F6C9C /mnt/wd; fi
it says "file exists".
Please help to correct the command. I'd like it to perform mount -U 84B46F79B46F6C9C /mnt/wd instead.
Thx ahead.
Last edited by marchelloUA; 01-23-2017 at 11:40 AM.
Sheesh, nested if's on a single line ... are you trying to make it hard to read or you just like the challenge??
I am not really following what you are trying to do? You perform the mount command whether it is mounted or not and also if the file does not exist. Why would you need to remount something
if a file does not exist??
It is starting to read like xy problem, ie. you are telling us something you want to do when really there is a bigger picture you are not telling us about and what you really want is something else.
Maybe ignore the code for a moment and explain what it is you are trying to achieve?
>>> grail
>>> Sheesh, nested if's on a single line ... are you trying to make it hard to read or you just like the challenge??
It will go to crontab, that's why it is on a single line. But you're right, it can be in script and readable.
>>> I am not really following what you are trying to do? You perform the mount command whether it is mounted or not and also if the file does not exist. Why would you need to remount something
if a file does not exist??
Well, I created that file for testing purpose. In my understanding, it should not exist if my drive is not mounted..
>>> It is starting to read like xy problem, ie. you are telling us something you want to do when really there is a bigger picture you are not telling us about and what you really want is something else.
I just need to ensure my drive is mounted and test file exists, otherwise I run mount command.
>>> Maybe ignore the code for a moment and explain what it is you are trying to achieve?
Good thing, I just explained it above and hope it is ok now. Sorry if it's still not.
>>> michaelk
>>> It appears that you have an underlying filesystem problem. What filesystem is on the USB drive and is it a mechanical or flash/SSD drive?
Do you also connect it to Windows and if so does it work?
Look at the output of the dmesg command and see what errors are associated with the drive if any.
If nothing on the drive is of any importance and you do not connect it to Windows then I would reformat it using an ext4 filesystem assuming the drive is still functional. If there is then we need to look into trying to recover your files.
$ ls /mnt/wd
ls: reading folder /mnt/wd: Input/output error
Quote:
$ if $(grep -q /dev/sdc1 /proc/mounts); then echo 'ok'; else mount -U 84B46F79B46F6C9C /mnt/wd; fi
ok
I'm afraid it doesn't work.. Just tested two commands below one by one, first shows that /mnt/wd is not mounted and the second should mount my device, but returns "ok".
How do I perform
Quote:
mount -U 84B46F79B46F6C9C /mnt/wd
if ls /mnt/wd returns ls: reading folder /mnt/wd: Input/output error ?
Last edited by marchelloUA; 01-23-2017 at 05:16 PM.
I'm afraid it doesn't work.. Just tested two commands below one by one, first shows that /mnt/wd is not mounted and the second should mount my device, but returns "ok".
How do I perform if ls /mnt/wd returns ls: reading folder /mnt/wd: Input/output error ?
You didn't get "ls" from me. Try again.
You want to mount this the same, every time? You will need to use the UUID parameter to do so, https://help.ubuntu.com/community/UsingUUID explains how.
First you ID the drive.
/dev/sdc1 is my mount.
yours is "/mnt/wd" whatever that is and you haven't specified.
Do you know how to find it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.