LinuxQuestions.org
Visit Jeremy's Blog.
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 04-05-2013, 12:22 PM   #1
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Rep: Reputation: Disabled
Question about conditional statements.


Hello everyone!

I am wondering if you can declare a variable inside a conditional statement, and then use that variable out side of the conditional statement.

Thank you!
 
Old 04-05-2013, 01:21 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
What programming language?

In C++ you cannot do that.
 
1 members found this post helpful.
Old 04-05-2013, 02:36 PM   #3
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for the reply.

Shell script.

Last edited by jason13131414; 04-05-2013 at 02:46 PM.
 
Old 04-05-2013, 04:21 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,962

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
For bash the answer is basically "yes", but if a block of commands is executed in a subshell, then changes to variables in that block are not communicated back to the parent shell. A parenthesized list explicitly forces execution to a subshell. Commands run in the background are also done in a separate process. A block of commands with its input or output directed through a pipeline also causes those commands to be executed in a subshell, and this is a frequent cause of problems in shell scripts.
Code:
N=0
echo hello world | {
    cat
    N=42   # This will not be seen in the parent shell
}
echo $N    # N still seen as 0 here
The code within an "if ... then ... fi" construct is also a block that could have its I/O pipelined, though doing so is pretty uncommon:
Code:
N=0
echo testing | if [[ $N == 0 ]]; then
    echo "I am here"
    cat
    N=22
fi
echo $N    # Still seen as 0
Without the pipeline, and even with simple (albeit unusual) redirection, the setting of N does occur in the main shell:
Code:
N=0
if [[ $N == 0 ]]; then
    echo "I am here"
    head
    N=55
fi </etc/profile
echo $N    # Has value 55

Last edited by rknichols; 04-05-2013 at 04:22 PM.
 
1 members found this post helpful.
Old 04-05-2013, 05:18 PM   #5
jason13131414
LQ Newbie
 
Registered: Apr 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for the response!!! Solved my problem!!!
 
  


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
question about command substitution and if statements mrb3nnio Linux - Newbie 3 04-02-2013 12:35 PM
Cannot get conditional statements to work in xDialog/Dialog Stevithen Programming 7 02-05-2011 07:03 PM
PHP & HTML conditional execution question bostonantifan Programming 1 05-19-2009 08:32 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM
conditional statements in gmake on Linux malik Linux - Software 0 04-28-2004 11:17 AM


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