LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2015, 05:47 PM   #1
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Rep: Reputation: 0
Bash: one-liner cd to dir1 or dir2, then execute command.


I'm trying to write a one liner(that's not obnoxiously long) to cd into either dir1 or dir2 and then run a command into which ever directory exists.

Something like this(pseudo code):
Code:
ssh remote_machine 'if [[ -d /opt/abc/def/ghi/dir1 || -d /opt/abc/def/ghi/dir2; then cd <which ever dir found>; fi; ./command.sh'
I could live with the length of the above if I could capture whether it was dir1 that was found or dir2 so I could change to it.

I think if I could find a clever use of change directory, I wouldn't even need the if conditional.

Any suggestions?
 
Old 08-06-2015, 06:10 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
How about something like this (ssh part not shown)...
(Modified and thanks to Keith Hedger too!)

Code:
(cd /opt/abc/def/dir1 || cd /opt/abc/def/dir2; ./command.sh)
This will let you see any error messages, but see next post to suppress those.

Last edited by astrogeek; 08-06-2015 at 06:21 PM. Reason: Simplified and acknowledge Keith Hedger!
 
2 members found this post helpful.
Old 08-06-2015, 06:16 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Try
Code:
( cd /tmp/w || cd /tmp/y;touch zz ) 2>/dev/null
It first trys to cd to /tmp/w if this fails it cds to /tmp/y and then touches the file zz
 
2 members found this post helpful.
Old 08-06-2015, 06:18 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
sorry astrogeek you posted just about the same as me as i was typing
 
Old 08-06-2015, 06:24 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by Keith Hedger View Post
sorry astrogeek you posted just about the same as me as i was typing
Ditto...
 
Old 08-06-2015, 06:31 PM   #6
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Original Poster
Rep: Reputation: 0
These are both great. Thanks so much.

Gary
 
Old 08-06-2015, 06:48 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Glad that helped!

It is worth noting that should BOTH cd's fail it will still try to run ./command.sh from the login user home directory. That file probably will not exist, but if it did... keep that in mind.

To avoid that possibility:

Code:
((cd /opt/abc/def/dir1 && ./command.sh) || (cd /opt/abc/def/dir2 && ./command.sh))

(full parenthesis optional, but good habit...)

Last edited by astrogeek; 08-06-2015 at 06:55 PM.
 
Old 08-06-2015, 06:56 PM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
To prevent the command being run if neither dir exists use
Code:
( cd /tmp/x || cd /tmp/y && touch zz ) 2>/dev/null
In this if both cd's fail the touch is not run if either of the two cd's succeed the touch is run.
Well spotted astrogeek.
 
Old 08-06-2015, 06:58 PM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
This is getting spooky now !
 
Old 08-06-2015, 07:00 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by Keith Hedger View Post
This is getting spooky now !
I think I hear the fat lady singing... no wait - it's Elvis!

Maybe great minds truly do think alike! Yea, that must be it...
 
  


Reply

Tags
bash, remote



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
copy only new files from dir1 to dir2 using c++ on Linux miamagoo Programming 23 08-18-2010 11:00 AM
BASH SHELL , how to copy all file in dir1 to dir3 CheeSen Programming 15 11-02-2008 06:11 AM
cp -al dir1/ dir2/ but skip some dir1/subdir Meson Linux - General 1 09-28-2008 01:39 PM
make a file in dir1 appear to be in writeprotected dir2 magicpio Linux - Newbie 2 09-25-2005 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 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
Open Source Consulting | Domain Registration