The "Linux questions (and other stuff)" thread


Last Updated: 2015-05-27 11:42:02
  1. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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 (Text):
    1. $ whereis fantasy_1.jpg
    2. fantasy_1:
    3. $ whereis fantasy_1
    4. fantasy_1:
    and then a program (backgammon) that also exists:

    Code (Text):
    1. $ whereis gnubg
    2. 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.

    Advertisement
    argedion likes this.
  2. argedion

    argedion The TechnoFrog VIP Member

    yeah just keep out all the racey steamy parts :D
  3. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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!
    argedion likes this.
  4. argedion

    argedion The TechnoFrog VIP Member

    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
  5. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    argedion, you're confusing me! :eek: :)

    Earlier you said:
    But then you said:
    :confused:
  6. argedion

    argedion The TechnoFrog VIP Member

    yes the install.sh will create the file that we need.
  7. nickdalzell

    nickdalzell Well-Known Member Contributor

    when did everyone start speaking in Greek?
  8. argedion

    argedion The TechnoFrog VIP Member

    was not aware we were
  9. nickdalzell

    nickdalzell Well-Known Member Contributor

    sounds Greek to me
  10. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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?
  11. 9to5cynic

    9to5cynic Well-Known Member

    What about a ls piped to grep? The exit code would reveal if it found it or not.....
  12. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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.
  13. argedion

    argedion The TechnoFrog VIP Member

    if serialno exist then we read a line from it and continue if it does not exist we run the bash to create it with a default user given value and then exit the bash to be run again. so

    if [[ -e ./$serialno ]] ; then
    get info into a read line
    else
    install.sh
    exit
    fi
  14. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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 (Text):
    1. apropos test
    at a prompt and skimming through its results? :)
    9to5cynic and argedion like this.
  15. argedion

    argedion The TechnoFrog VIP Member

    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.
  16. MoodyBlues

    MoodyBlues - Crazy peacock person - Guide

    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 (Text):
    1. ...
    2. TAP::Parser::Result::Test (3perl) - Test result token.
    3. TAP::Parser::Scheduler (3perl) - Schedule tests during parallel testing
    4. TAP::Parser::Scheduler::Job (3perl) - A single testing job.
    5. [b]test (1)             - check file types and compare values[/b]
    6. Test::Builder (3perl) - Backend for building test libraries
    7. Test::Builder::Module (3perl) - Base class for test modules
    8. Test::Builder::Tester (3perl) - test testsuites that have been built with Test::Builder
    9. Test::Builder::Tester::Color (3perl) - turn on colour in Test::Builder::Tester
    10. Test::Harness (3perl) - Run Perl standard test scripts with statistics
    11. Test::More (3perl)   - yet another framework for writing test scripts
    12. Test::Simple (3perl) - Basic utilities for writing tests.
    13. Test::Tutorial (3perl) - A tutorial about writing really basic tests
    14. testdisk (1)         - Scan and repair disk partitions
    15. testkeys (1)         - returns SDL keycode for key pressed.
    16. testparm (1)         - check an smb.conf configuration file for internal correctness
    17. testparm.samba3 (1)  - check an smb.conf configuration file for internal correctness
    18. ...
    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 (Text):
    1. 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.
  17. 9to5cynic

    9to5cynic Well-Known Member

    ^ Kind of in the same vein, but man -k <term> allows you to search for man pages for something.

    I always use that to remember the xxd command ( I think that's the one). What ever I'm thinking of allows you to convert ascii to binary or octal (probably hex too)... very useful for say - avatars :D :p
  18. argedion

    argedion The TechnoFrog VIP Member

    how do i get pigin to let me use gtalk? Using Gnome 3 on fedora 17
  19. saptech

    saptech Well-Known Member

    argedion likes this.
  20. argedion

    argedion The TechnoFrog VIP Member

    yep that is the way i did it however i keep getting an unable to connect error I've double and triple checked my password to make sure it was right
  21. saptech

    saptech Well-Known Member

    Hmmm, that is odd. I just added it to my Pidgin without any issues. I have been using Mageia 3 Beta but yesterday booted into Salix 14. I wanted to update it and use it awhile.

    Again, adding Gtalk worked without any problems.
  22. argedion

    argedion The TechnoFrog VIP Member

    some progress i'm getting available but not showing anyone on my list
  23. argedion

    argedion The TechnoFrog VIP Member

    spoke to soon :(
  24. Bob Cat

    Bob Cat Careful, They're sharp!! Moderator

    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?
  25. palmtree5

    palmtree5 Sunny Vacation Supporter! Moderator

    I may be wrong but I believe Ubuntu installed with Wubi is slower than if you were to manually set up a dual boot (i.e. partitioning the drive, etc) because it has to access its virtual drive through the physical drive.
    argedion likes this.

Share This Page