LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-17-2010, 12:27 AM   #1
bdrockwell
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
Bash script variable NULL instead of value of expression


Quote:
#!/bin/sh

for i in {1..10}
do
for j in {1..50}
do
echo `host 129.74.$i.$j` | awk '{print $5}'

set domain = `host 129.74.$i.$j` | awk '{print $5}'

echo $domain
done
done
The first echo generates something like: abc.de.fgh
The second echo generates:

Any help would be highly appreciated.
 
Old 11-17-2010, 12:34 AM   #2
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
You just define the variable without set, like so:

Code:
#!/bin/sh

for i in {1..10}
do
for j in {1..50}
do
echo `host 129.74.$i.$j` | awk '{print $5}'

domain = `host 129.74.$i.$j` | awk '{print $5}'

echo $domain
done
done

Last edited by Kenny_Strawn; 11-17-2010 at 12:35 AM.
 
0 members found this post helpful.
Old 11-17-2010, 01:41 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@bdrockwell: If you want to fill a variable with the output of a command:

domain="`host 129.74.$i.$j | awk '{print $5}'`"

Or

domain=$(host 129.74.$i.$j | awk '{print $5}')

@Kenny_Strawn: Removing the set part is not enough to fix the problem.......

Hope this helps
 
Old 11-17-2010, 01:42 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In assigments you cannot have spaces around the equal sign:
Code:
domain=`host 129.74.$i.$j | awk '{print $5}'`
moreover the command substitution operator (backticks) must include the entire command line (included the pipe and the awk command).
 
  


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] [bash] assign boolean expression to variable hashbang#! Programming 10 08-16-2009 08:44 AM
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
Handling null options in a bash script which ought to have a value dairo Linux - General 2 03-16-2009 12:27 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
bash shell script expression CowboyJ Programming 2 04-24-2005 11:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:40 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