LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-08-2010, 06:03 AM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Fedora 14, Ubuntu , Slax 5.1.8, OpenSolaris, Centos 4.8
Posts: 296

Rep: Reputation: 30
Trouble storing a variable in a shell script after awk output


Hi folks,

On command line I have no problem storing a variable e.g

Code:
:~/bin$ process=`ps -ef | awk ' $8 == "idesk" { print $2 }'`
:~/bin$ echo $process
26736
:~/bin$
When I try to incorporate this into a shell script I get a blank.

Code:
 :~/bin$ cat process_idesk
#!/bin/bash

process=`ps -ef | awk ' $8 == "idesk" { print $2 }'`
Code:
 :~/bin$  ./process_idesk
:~/bin$ echo $process

:~/bin$
The process_idesk script has been chmoded to be executable by the user. I'm sure there must be a silly omission on my behalf.

Thanks

C
 
Old 02-08-2010, 06:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The script runs in its own (child) shell. After the script is finished, you return to your initial (mother) shell.

The process variable is filled in the child shell and will not be available in the mother shell after execution.

Put the echo $process in the script and it will echo what you expect. If you need the use the variable outside the script you need to store it first (to file) to be able to use it afterwards.

Maybe this will help: Advanced Bash-Scripting Guide - Chapter 20. Subshells

Hope this clears things up.
 
Old 02-08-2010, 06:35 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Fedora 14, Ubuntu , Slax 5.1.8, OpenSolaris, Centos 4.8
Posts: 296

Original Poster
Rep: Reputation: 30
Ah I see ! That explains it. It makes sense now. Thanks druuna !

C
 
Old 02-08-2010, 07:13 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
One solution is to source the script -- then all the variables set in the script are defined in the parent shell.
Code:
source ./process_idesk

Last edited by catkin; 02-08-2010 at 07:14 AM. Reason: Typo
 
  


Reply

Tags
script, variables


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
shell script: saving output from command into a variable grcunning Linux - Newbie 7 01-26-2010 11:43 AM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 02:08 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM


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