LinuxQuestions.org
Review your favorite Linux distribution.
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 04-02-2013, 08:59 AM   #16
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286

It looks a text file more than a script. Since no command(s) are specified in it, so it's won't do anything. There is no scope of these variables outside of script.

If you want to create directories, write following commands at the end of your script, like this:
Code:
#!/bin/bash
........
........
# Hadoop Service - HDFS
 #
# Space separated list of directories where NameNode will store file system image.
 DFS_NAME_DIR="/grid/hadoop/hdfs/namenode /grid1/hadoop/hdfs/namenode"

# Space separated list of directories where DataNodes will store the blocks.
 DFS_DATA_DIR="/grid/hadoop/hdfs/datanode /grid1/hadoop/hdfs/datanode /grid2/hadoop/hdfs/datanode"
........
........ 
mkdir -p $DFS_NAME_DIR
chmod -R 755 $DFS_NAME_DIR
echo "Directories $DFS_NAME_DIR created." 
 
Old 04-02-2013, 07:13 PM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As above, vars go out of scope when a script completes, so either

1. add your cmds to the bottom as above
OR
2. source the file so it actually sets the vars in your current env eg
Code:
. ./script

#OR
source ./script
then do your mkdir etc.
 
Old 04-03-2013, 12:18 AM   #18
turboscrew
Member
 
Registered: Apr 2009
Location: Nokia (town), Finland
Distribution: Mint, Debian
Posts: 601

Rep: Reputation: 46
Like I mentioned:

Quote:
Originally Posted by rushikeshgaradade View Post
DFS_NAME_DIR="/grid/hadoop/hdfs/namenode /grid1/hadoop/hdfs/namenode"
Here a new shell is started to run the command. The variable "DFS_NAME_DIR" becomes defined in this shell's environment. When the script ends, the shell exits and the environment is gone. You can see that by adding "printenv | grep DFS_NAME_DIR" to the end of the script.
Then give that same command on the terminal after the script is done.

If you want that variable to be seen in your command-shell, you have to "source" the script
("." is shorthand for "source").
Code:
source my_script.sh
. my_script.sh
Quote:

i have executed it with
#bash dierctories.sh

but i didnt find any affect of it
If you had the variable defined in this shell's environment, the shell executing your
script "directories.sh" doesn't have that variable in its environment unless the variable is exported.
Code:
export DFS_NAME_DIR="/grid/hadoop/hdfs/namenode /grid1/hadoop/hdfs/namenode"
Exported variables are "forwarded" to "sub-shells".
Quote:
and also it is said

mkdir -p $DFS_NAME_DIR

but this command showing error:

mkdir: missing operand
Try `mkdir --help' for more information.



Does this mean that directories.sh file is not executed. or is there any other alternative for "DFS_NAME_DIR"

Please help me.........
That's why there is no variable "DFS_NAME_DIR" in your shell environment and the command:
"mkdir -p $DFS_NAME_DIR" becomes "mkdir -p"

Last edited by turboscrew; 04-03-2013 at 12:23 AM. Reason: typos
 
Old 04-03-2013, 01:22 AM   #19
rushikesh24
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Rep: Reputation: Disabled
thanks...

actually i need these variables outside the scripts also....
i have to execute the commands like mkdir -p DFS_NAME_DIR etc.
As u said these variables don't hold the values after running the scripts... So can we make the variables to hold the values permanently i.e. outside the scripts also.
 
Old 04-03-2013, 01:39 AM   #20
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by rushikesh24 View Post
As u said these variables don't hold the values after running the scripts... So can we make the variables to hold the values permanently i.e. outside the scripts also.
Did you read the above posts? I don't think so. If you want those variables outside of your script, then use following cmds:
Code:
~$ source myscript.sh
OR
~$ . myscript.sh
 
Old 04-03-2013, 01:43 AM   #21
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by rushikesh24 View Post
So can we make the variables to hold the values permanently i.e. outside the scripts also.
Yes, as explained by others you do this by sourcing the script instead of executing it.
Eg instead of running:
Code:
foo.sh
you need
Code:
source foo.sh
or
Code:
. foo.sh
Note however that if you want the variables available to new processes (not just he shell you are currently in), you need to export the variables in the script. Eg

Eg Instead of
Code:
FOO="/bar/baz"
you can do
Code:
export FOO="/bar/baz"
Evo2.
 
Old 04-03-2013, 02:29 AM   #22
rushikesh24
LQ Newbie
 
Registered: Mar 2013
Posts: 11

Rep: Reputation: Disabled
hey thanks all.... n sorry.. i didnt refresh my page so didnt get new answers posted by you all..
 
Old 04-05-2013, 12:31 AM   #23
turboscrew
Member
 
Registered: Apr 2009
Location: Nokia (town), Finland
Distribution: Mint, Debian
Posts: 601

Rep: Reputation: 46
Quote:
Originally Posted by evo2 View Post
Hi,

that script, as posted, does nothing but set some variables that will go out of scope as soon as it exits. Ie it does absolutely nothing. Is that really the whole script?

Evo2.
...unless it's sourced, but even then the parameters are not exported to the shells running other commands.

---------- Post added 04-05-13 at 08:32 AM ----------

"source" is a shell built-in command.
Add "export" in front of the variable definitions and source the script.
Then try running the commands.
 
  


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] C++ multiple variable declaration error, even after I set #ifndef preprocessor chinho Programming 4 01-18-2011 04:37 AM
declaration a variable in one file and initializing in another C program jamesbon Programming 5 11-25-2010 07:06 AM
Variable Declaration in shell scripts... vinaytp Linux - Newbie 4 02-04-2010 10:39 AM
Need to extract name of C variable from C declaration. judgex Programming 3 09-22-2007 12:46 PM
Using Flex and Bison to parse the variable declaration section in Pascal unreal07 Programming 1 03-10-2007 08:25 PM

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

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