LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-03-2013, 10:01 PM   #1
ReaperX7
LQ Guru
 
Registered: Jul 2011
Location: California
Distribution: Slackware64-15.0 Multilib
Posts: 6,558
Blog Entries: 15

Rep: Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097
Question Using a command sequence


I'm currently crafting a script to use a command to check for a file before proceeding with execution of a task. I'm not very skilled with writing a script using bash, and various other tools, as usually tools like "Find" simply outputs and then stops execution.

How can I use the bash shell to generate an output that checks for a file using for example an if/else sequence, and then executes one of two commands in sequence such as this sample script (please correct as needed)

Code:
if [ -h /usr/lib/libX11.so ]; then
    echo "Found file libX11.so"
        else
        echo "Could not find libX11.so"
fi
Thank you.

Last edited by ReaperX7; 10-03-2013 at 10:02 PM.
 
Old 10-04-2013, 01:20 AM   #2
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by ReaperX7 View Post
I'm currently crafting a script to use a command to check for a file before proceeding with execution of a task. I'm not very skilled with writing a script using bash, and various other tools, as usually tools like "Find" simply outputs and then stops execution.

How can I use the bash shell to generate an output that checks for a file using for example an if/else sequence, and then executes one of two commands in sequence such as this sample script (please correct as needed)

Code:
if [ -h /usr/lib/libX11.so ]; then
    echo "Found file libX11.so"
        else
        echo "Could not find libX11.so"
fi
Thank you.
You are testing if that is a symbolic link, right?

Other than a funny indent for "else" your code looks OK. I would write it like:
Code:
if [ -h /usr/lib/libX11.so ]; then
    echo "Found file libX11.so"
else
    echo "Could not find libX11.so"
fi
Then insert the command you want to run at the appropriate point
Code:
if [ -h /usr/lib/libX11.so ]; then
    echo "Found file libX11.so"
    run_me_if_file_exists
else
    echo "Could not find libX11.so"
    run_me_if_file_not_found
fi
run_me_in_either_case
 
Old 10-04-2013, 03:37 AM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
well to check if file exists there are multiple switches that can be used:

Code:
#to check if file exists and a regular file:
[ -f /location/file-name ] && echo -e "file found and is a regular file" || echo -e "file not found"

#to check if file exists and non empty:
[ -s /location/file-name] && echo -e "file found and not empty" || echo -e "file not found"

#to check if file exists:
[ -e /location/file-name ] && echo -e "file found" || echo -e "file not found"
these are simple tests that can be done and you can incorporate same with "if" too (just for FYI).

Last edited by SAbhi; 10-04-2013 at 03:40 AM.
 
  


Reply



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] tail command syntax to print a sequence of line rhadmn Linux - General 6 12-22-2010 06:02 AM
Using dd command to create a .sequence file zeus65 Linux - Newbie 4 10-05-2009 04:38 AM
Are results sent to an output file only at the end of a command sequence? Zebe Linux - Newbie 2 03-19-2009 06:34 PM
Ubuntu Server 7.04 command line boot sequence KAOZ_IT Linux - Server 1 07-05-2007 04:28 AM
Trying to make keyboard sequence execute a command in Xorg ghrellin Linux - Software 1 09-02-2005 10:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:29 AM.

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
Open Source Consulting | Domain Registration