Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use terminal response/output as IF/THEN argument? 2

Status
Not open for further replies.

GusGrave

Programmer
Nov 17, 2010
41
SE
I'm working on a basic script to help my co-workers with compiler choices on Mac OS X. I have a working script, but there is room for much improvement.

Is it possible to use a response for, for example: which gcc
as an argument for if/then in the script, and how would this be structured?

Say that the script runs 'which gcc' and the response is /usr/bin/gcc (Xcode distribution), I would like to use this as follows:

which gcc
if "response"=/usr/bin/gcc; then
echo "Using Xcode gcc"

else

if "responce"=/usr/local/bin/gcc; then
echo "Using HPC gcc"

fi
fi

Right now the script is only checking for the presence of files and using that as arguments, but this isn't a particularly nice solution:

file=/usr/bin/gcc
if [ -f $file ] ; then
echo "Xcode gcc installed".............

Best regards
Gustaf
 
Hi

You not specified which shell are you using, but I would try something like this :
Code:
[highlight][navy]response[/navy][teal]=[/teal][green][i]"$( [/i][/green][/highlight][green][i]which gcc[/i][/green][highlight][green][i] )"[/i][/green][/highlight]
[b]if[/b] [highlight][teal][[/teal][/highlight] [green][i]"[highlight]$[/highlight]response"[/i][/green][highlight] [/highlight][teal]=[/teal][highlight] [/highlight][green][i]'/usr/bin/gcc'[/i][/green] [teal][highlight]][/highlight];[/teal] [b]then[/b]
  echo [green][i]"Using Xcode gcc"[/i][/green]
[b]elif[/b] [highlight][teal][[/teal][/highlight] [green][i]"[highlight]$[/highlight]response"[/i][/green][highlight] [/highlight][teal]=[/teal][highlight] [/highlight][green][i]'/usr/local/bin/gcc'[/i][/green] [teal][highlight]][/highlight];[/teal] [b]then[/b]
  echo [green][i]"Using HPC gcc"[/i][/green]
[b]fi[/b]

Feherke.
 
Another way:
Code:
case $(which gcc) in
/usr/bin/gcc) 
  echo "Using Xcode gcc";;
/usr/local/bin/gcc)
  echo "Using HPC gcc";;
*)
  echo "Unknown compiler";;
esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry, beginner mistake I guess ;)

Since the default shell on Mac is bash, that is what I'm using. There is a varying degree of computer skills present so a minimal number of steps is always worth striving for.

But this is excellent, now I have something to work with! Thank you very much for your help!

I was thinking about adding a sed string to modify the .profile file as well, this has to be done manually right now. But thats another post....

// Gus
 
Well, thank you PHV as well!

Now I have a sturdy foundation so I'm bound to get something working with both of your kind input!

Best of regards to both
// Gus
 
Hi

Another way :
Code:
[COLOR=chocolate]declare[/color] -A [navy]compiler[/navy][teal]=([/teal] [teal][[/teal]/usr/bin/gcc[teal]]=[/teal][green][i]'Xcode gcc'[/i][/green] [teal][[/teal]/usr/local/bin/gcc[teal]]=[/teal][green][i]'HPC gcc'[/i][/green] [teal])[/teal]
echo [green][i]"Using ${compiler[$(which gcc)]:-unknown compiler}"[/i][/green]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top