LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-23-2017, 07:54 AM   #1
marchelloUA
LQ Newbie
 
Registered: Nov 2014
Posts: 27

Rep: Reputation: Disabled
Question 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.
 
Old 01-23-2017, 08:04 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,002

Rep: Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098Reputation: 4098
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.
 
Old 01-23-2017, 08:06 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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.
 
Old 01-23-2017, 08:11 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651Reputation: 1651
I like using the proc directory.

Code:
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
 
Old 01-23-2017, 10:29 AM   #5
marchelloUA
LQ Newbie
 
Registered: Nov 2014
Posts: 27

Original Poster
Rep: Reputation: Disabled
Question

Something is missing probably.

I just got
Code:
$ 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 10:40 AM.
 
Old 01-23-2017, 11:16 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
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?
 
Old 01-23-2017, 11:18 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,935

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
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?
 
Old 01-23-2017, 12:24 PM   #8
marchelloUA
LQ Newbie
 
Registered: Nov 2014
Posts: 27

Original Poster
Rep: Reputation: Disabled
>>> 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?

It is ntfs, mechanical USB western digital drive.
 
Old 01-23-2017, 12:41 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,935

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
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.
 
Old 01-23-2017, 12:54 PM   #10
marchelloUA
LQ Newbie
 
Registered: Nov 2014
Posts: 27

Original Poster
Rep: Reputation: Disabled
>>> michaelk
>>> Do you also connect it to Windows and if so does it work?

I do not connect it to Windows and can't check it because I got no Windows near by.

>>> Look at the output of the dmesg command and see what errors are associated with the drive if any.

Will do and will update, thx.
 
Old 01-23-2017, 01:17 PM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by marchelloUA View Post
it says "file exists".

Please help to correct the command. I'd like it to perform mount -U 84B46F79B46F6C9C /mnt/wd instead.
Thx ahead.
Code:
if $(grep -q /dev/sdc1 /proc/mounts); then something; else something mount -U 84B46F79B46F6C9C /mnt/wd; fi
See also https://help.ubuntu.com/community/UsingUUID
http://www.linuxquestions.org/questi...8/#post3066998
https://duckduckgo.com/?q=test+for+m...estions&ia=web

Last edited by Habitual; 01-23-2017 at 01:25 PM.
 
Old 01-23-2017, 01:21 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,935

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
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.
 
Old 01-23-2017, 04:15 PM   #13
marchelloUA
LQ Newbie
 
Registered: Nov 2014
Posts: 27

Original Poster
Rep: Reputation: Disabled
>>> Habitual

Quote:
$ 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 04:16 PM.
 
Old 01-23-2017, 04:41 PM   #14
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by marchelloUA View Post
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?

http://xyproblem.info/
and/or
https://www.linuxquestions.org/quest...llected-35954/

Last edited by Habitual; 01-23-2017 at 04:48 PM.
 
Old 01-23-2017, 09:29 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
If the grep is returning that it is mounted, assuming you are putting the correct drive name in, then maybe you could show us the output of the grep?
 
  


Reply

Tags
error, input, output


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash scripting to find CPU info and perform action DavidDiepUSC Linux - Newbie 10 03-09-2013 04:55 AM
Script to check for text file is different and perform certain action depam Linux - Newbie 3 11-26-2012 09:54 PM
[SOLVED] Bash: Need help with an if statement to check if a file exists or not. MTAS Programming 16 10-26-2010 12:08 PM
[SOLVED] bash - check if file exists in tree hashbang#! Programming 10 01-21-2010 01:37 PM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 12:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration