- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- variables in bash
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2004 10:05 PM
03-06-2004 10:05 PM
variables in bash
I'm using bash in r.h. AS3.0 .
in the script I declare:
VAR=5
then I run a while loop that adds data to the variable, but after the loop the variable returns to be 5.
how can I make it save the changes I have done in the loop. in sh in HP-UX11i it works, but here it doesn't work.
thanx,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2004 10:12 PM
03-06-2004 10:12 PM
Re: variables in bash
like export
to add a variable to a shell (across logouts):
add a line to /etc/bashrc (for all users)
or
to ~/.bashrc for a specific user
then do logout and login..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2004 11:04 PM
03-06-2004 11:04 PM
Re: variables in bash
A bit ugly, isn't it?
These scripts work fine in the bash of HPUX, what's the difference? Some flag maybe?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 01:43 AM
03-07-2004 01:43 AM
Re: variables in bash
have a look at the examples in
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html#ss7.1
Works for me on Linux. If you still have problems with the script after reading this please do post a >>short<< reproducer here.
Greetings, Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 02:23 AM
03-07-2004 02:23 AM
Re: variables in bash
Can you post the troublemaking part of your script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 05:13 PM
03-07-2004 05:13 PM
Re: variables in bash
I would assume that you have a slight loginc problem somewhere, could you post the relevant part of the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 07:22 PM
03-07-2004 07:22 PM
Re: variables in bash
here the script:
x=0
v="hello"
cat /etc/fstab | while read line
do
x=`echo $x+1 | bc`
v=$v$line echo $v
done
echo $v
something like this, the output I get:
helloline1
helloline1line2
helloline1line2line3
.
.
.
hello
as u can see, outside the loop, "v" stayed unchanged ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 07:45 PM
03-07-2004 07:45 PM
Re: variables in bash
IFS="
"
x=0;
v="hello"
for line in `cat /etc/fstab`
do
(( x = x + 1 ))
v=$v$line
echo "$v"
done
echo "LAST ONE $v"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 09:24 PM
03-07-2004 09:24 PM
Re: variables in bash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2004 09:43 PM
03-07-2004 09:43 PM
Re: variables in bash
If behaves as if the "cat /etc/fstab | while read line" is creating a subshell which it may well do. However, if this is the case, then increasing the value of "x" at initialization would have no effect but it does. Also, exporting "v" would solve the problem but it doesn't.
On the other hand, I've never really trusted piping into my own script so maybe I'll stick with that distrust :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2004 11:51 AM
03-08-2004 11:51 AM
Re: variables in bash
Basically, any commands executed in pipe are executed as a SubShell, thus environment variables changed within the pipe (read: subshell) stay there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2004 05:22 PM
03-08-2004 05:22 PM
Re: variables in bash
This is how it looked to me too BUT if it really is being run in a sub-shell, you would expect that setting $x to say 10 and not exporting it would have no effect on the loop in the pipe. However, it does. *shrug*
I would be interested to know what is "correct". Personally I think that "bash" is wrong here because the posix complient shell on hpux does not have this behaviour. It works as you would expect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2004 03:24 PM
03-09-2004 03:24 PM
Re: variables in bash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2004 06:26 PM
03-09-2004 06:26 PM
Re: variables in bash
I'm runnig sh on both of them, HPUX and linux.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2004 10:04 AM
03-10-2004 10:04 AM
Re: variables in bash
It won't increment it within a piped loop on Linux (using bash), or using pdksh.
As for other shells, I can't guarantee.
sh/ksh on HPUX is a different story.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2004 04:12 PM
03-10-2004 04:12 PM
Re: variables in bash
your routine there could do what you expect it if you not use a pipe, i.e.
while read line
do
done < /etc/fstab