• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

The "Linux questions (and other stuff)" thread

there is also the 'whereis' command that is a lot less complicated:

$>whereis startx

bash: /usr/X11/startx

if the file did not exist it would be like:

$>whereis foo

bash: no such file or directory
That's not a good method for testing the existence of a file within a script. That's what the bash (and, waaaaaaaay before bash, the Bourne shell) test function is for.

ETA: I was in a hurry when I wrote the above. Now I want to expand on WHY it's not a good method.

The whereis command is intended to "locate the binary, source, and manual page files for a command," according to its man page. It's basically searching the file system, whereas the bash test command is told where to look, and then just has to see if a specific file is there or not. Also, my output for whereis looks like this, using a file [which DEFINITELY exists] from argedion's project:

Code:
$ whereis fantasy_1.jpg
fantasy_1:
$ whereis fantasy_1
fantasy_1:

and then a program (backgammon) that also exists:

Code:
$ whereis gnubg
gnubg: /usr/lib/gnubg /usr/games/gnubg /usr/share/gnubg /usr/share/man/man6/gnubg.6.gz

So, for one thing, when it fails, it doesn't give a consistent output that could be used in a program. But the bigger issue--and the WORST issue--is that it fails even though a file exists! :eek: In my example, it's a JPEG, but the point is that whereis is used to "locate the binary, source, and manual page files for a command," not just any random file type.
 
  • Like
Reactions: argedion
Upvote 0
yeah just keep out all the racey steamy parts :D
Ah, crap...do I HAVE to?! ;)

Okay, here's what argedion asked in a PM--minus the steamy stuff, of course:

I need to find out if a file exist or not in a bash script i have tried doing a few routines but have failed miserably. if the file does not exist i need to create it and have the value for the file set. at the moment the file only has one line of information that is a path.

if [[ "./$serialno" =~ " " ]] ; then
install.sh
fi

exec < ./$serialno

the above will give me an error at the exec if the file doesnt exist even though i'm trying to exit the script and run another script to create the file with the default user value.

Looking strictly at

I need to find out if a file exist or not in a bash script
...
if the file does not exist i need to create it and have the value for the file set


this is a piece of cake. But in your sample code I'm not clear on what's what. Are you testing the existence of a file named serialno in the current directory? And then, if it exists, you're running install.sh? Please clarify!
 
  • Like
Reactions: argedion
Upvote 0
Ah, crap...do I HAVE to?! ;)

Okay, here's what argedion asked in a PM--minus the steamy stuff, of course:



Looking strictly at

I need to find out if a file exist or not in a bash script
...
if the file does not exist i need to create it and have the value for the file set


this is a piece of cake. But in your sample code I'm not clear on what's what. Are you testing the existence of a file named serialno in the current directory? And then, if it exists, you're running install.sh? Please clarify!

serial number is the name of a file no extention. and yes its in the current directory of the current bash file. if the file does not exist then we want to run the install.sh file. My post earlier was the result of me finding out the proper way to do it.

if [[ -e "filename" ]] ; then
true
else
false
fi

unless you have a more sure fire way of doing it
 
Upvote 0
argedion, you're confusing me! :eek: :)

Earlier you said:
I need to find out if a file exist or not in a bash script
...
if the file does not exist i need to create it and have the value for the file set

But then you said:
serial number is the name of a file no extention. and yes its in the current directory of the current bash file. if the file does not exist then we want to run the install.sh file.

:thinking:
 
Upvote 0
yes the install.sh will create the file that we need.
Okay, so at this point all you really need is to see if file serialno exists in the current directory and, if it does, execute install.sh, correct? What do you want to happen if serialno does not exist?
 
Upvote 0
What about a ls piped to grep? The exit code would reveal if it found it or not.....
That would work, but it's a more roundabout way of doing what test does cleanly. You'd have to read the exit code and then continue depending on whether its result is 0 or 1.
 
Upvote 0
recently working on a project that called to find out if a file existed or not I had no idea of how to do this in Linux. Not being always the sharpest tool in the shed I tried several ways and failed miserably. I searched a little but since I suck at searching (never know really what to ask)
Are you aware of the apropos command? It's very handy for searching for commands when you're not exactly sure what you're searching for.

In your scenario, you knew you wanted to test for the existence of a file, so how about typing:

Code:
apropos test

at a prompt and skimming through its results? :)
 
Upvote 0
cool command not sure how I would have used it to help me with the particular problem I was having but nice to know its there.
You use it in combination with other things. For example, in your situation, first you'd run apropos test, which would yield a list of likely commands:

Code:
...
TAP::Parser::Result::Test (3perl) - Test result token.
TAP::Parser::Scheduler (3perl) - Schedule tests during parallel testing
TAP::Parser::Scheduler::Job (3perl) - A single testing job.
[b]test (1)             - check file types and compare values[/b]
Test::Builder (3perl) - Backend for building test libraries
Test::Builder::Module (3perl) - Base class for test modules
Test::Builder::Tester (3perl) - test testsuites that have been built with Test::Builder
Test::Builder::Tester::Color (3perl) - turn on colour in Test::Builder::Tester
Test::Harness (3perl) - Run Perl standard test scripts with statistics
Test::More (3perl)   - yet another framework for writing test scripts
Test::Simple (3perl) - Basic utilities for writing tests.
Test::Tutorial (3perl) - A tutorial about writing really basic tests
testdisk (1)         - Scan and repair disk partitions
testkeys (1)         - returns SDL keycode for key pressed.
testparm (1)         - check an smb.conf configuration file for internal correctness
testparm.samba3 (1)  - check an smb.conf configuration file for internal correctness
...

You scan down the commands, looking at their names and their [very] brief descriptions, then when you see something that might apply to your situation, you can proceed. For example, you could:

Code:
man test

Once you're reading the man (manual) pages for test, you'll see that it does exactly what you need. Then you see how its syntax works and, voila!, you're good to go.
 
Upvote 0
Ok so I installed the newest Ubuntu on my laptop with the windows installer. It's really slow. Seems like it should be a lot faster than what it is. In fact during the installation, it froze and I had to reboot the computer and then it installed ok. So after it finished installing I tried to download a couple things, one of them being chroma. It was so slow like it's out of memory or something and I finally got it to restart. When it did what I downloaded wasn't there anymore. After rebooting it's not as slow. I used Ubuntu on a different computer back when I think it was 10.10 and it was fast. So what am I doing wrong?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones