LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-16-2004, 01:12 PM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
bash script variables


Hi there,

I have a very very simple bash script:

#!/bin/bash

mail=`/usr/bin/mail`

echo "Hello there" | $mail -s "testing" twantrd
exit 0

However, run trying to run it..it will give me "-s: command not found". If I replace $mail with just the command 'mail' it will work. Any ideas why?

Also with ssh:

#!/bin/bash
ssh=`/usr/local/ssh`
$ssh localhost

It will give me this when trying to run: "./ssh_script: localhost: command not found"

However, if I replace $ssh with just the command 'ssh' then it works.
Why doesn't declaring these certain commands as variables work while commands such as 'date', 'hostname', etc.. work as variables? Hmmm...

-twantrd
 
Old 11-16-2004, 01:17 PM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Have you tried using normal quotes around /usr/bin/mail?

And execute the program.

#!/bin/bash

mail="/usr/bin/mail"

echo "Hello there" | $($mail -s "testing" twantrd)
exit 0

Last edited by ahh; 11-16-2004 at 01:20 PM.
 
Old 11-16-2004, 05:55 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Original Poster
Rep: Reputation: 52
Ahhh thank you. DUH, that makes sense! Thanks!!

-twantrd
 
Old 11-16-2004, 07:44 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Original Poster
Rep: Reputation: 52
Actually, I'm back again . Ok so:

ssh="/usr/bin/ssh" works
ssh=`/usr/bin/ssh` does not work

But how come:
hostname=`/usr/bin/hostname`works

How come hostname works when you declare it as a variable and ssh doesn't?? Thanks!

-twantrd
 
Old 11-17-2004, 01:12 AM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
I think this is whats happening:-

When you use
Code:
hostname=`/usr/bin/hostname`
bash executes /usr/bin/hostname and stores the output in $hostname.
Then when you call $hostname in a script it will be replaced with the hosts name, in my case desktop, which is the same as executing the program hostname.

When you use
Code:
ssh=`/usr/bin/ssh`
bash executes /usr/bin/ssh (without any parameters) and stores the output in $ssh.
Then when you call $ssh in a script it will be replaced with the output from ssh called with no parameters,
Code:
usage: ssh [-1246AaCfghkNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
           [-D port] [-e escape_char] [-F configfile] [-i identity_file]
           [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]
           [-p port] [-R port:host:hostport] [user@]hostname [command]
which is obviously not the same as executing the program ssh with any parameters you may have added.

You could test this by placing an
Code:
echo $ssh
in your script.
 
Old 11-17-2004, 01:16 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,493

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Because hostname is a non interactive command that returns a string that is affected to the variable value, while ssh is an interactive command that returns nothing when launched without options or arguments. It should return its help message on stderr though, but this is not going to the variable. mail is also returning nothing on stdout, thus the initial script problem.
Single and double quotes are used to group strings, while back-quotes are executing the embedded string.
Back quotes are becoming obsolete and should be replaced by the equivalent syntax:
hostname=$(hostname)
 
Old 11-17-2004, 01:46 AM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Original Poster
Rep: Reputation: 52
Ok, now I fully understand. Thanks to you both!!!


-twantrd
 
Old 11-17-2004, 02:38 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
It has nothing to do with the program being interactive or not.

var=`/usr/bin/program`
or
var=$(/usr/bin/program)
means execute /usr/bin/program and store the output
It's more or less like a pointer on char in C (char*), the value of which is computed by calling a function.

var='/usr/bin/program'
or
var="/usr/bin/program"
means store the string (value) "/usr/bin/program" into var.
It's more or less like a pointer on a function in C (char*), though in this case it can perfectly be handled as a char*, as in: echo $var.

Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 06:19 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration