Visit Our UNIX and Linux User Community


Two curious questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two curious questions
# 1  
Old 10-03-2012
Two curious questions

Hi,

I have been thinking about a few things that I have no idea of how to do with a scripting language (awk/sed I know to make proper use of just these 2).

1. Is there a way to have persistent variables? Say a variable that will be held in memory, and which can be accessed by subsequent scripts, without having to write it to a file and re use it later.

2. Is there a way to retrieve results of a command before a pipe, say for example
Code:
cat filex | awk '{print $2}' | awk '{sum+=$1}END{print sum}'

is there a way that I can get to see the $2 values without writing to a file?

Probably both of these questions can be answered by using different scripts that do more than one function, I just wanted to know if there is a in-built/ default way of doing this.
# 2  
Old 10-03-2012
I don't know what you are trying to do exactly, but I think this is what you want.

1. use export to make variables public:
Code:
var=test
export var

2. The only way I know is to store it in a variable:
Code:
cat filex | var=$(awk '{print $2}') ; echo $var | awk '{sum+=$1}END{print sum}'

# 3  
Old 10-03-2012
Hi,

Thanks for the response, I am not doing anything specifically, I was just curious.

I have used export before...y didnt I think of that! Thanks.
# 4  
Old 10-03-2012
If you want to "see" the intermediate results on your screen, put a | tee /dev/tty | into your pipe.
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 10-03-2012
Quote:
Originally Posted by jamie_123
1. Is there a way to have persistent variables? Say a variable that will be held in memory, and which can be accessed by subsequent scripts, without having to write it to a file and re use it later.
No, there's not a "global" kind of variable. Child processes inherit exported variables from their parents, but it goes no further than that. This is why we have things like /etc/profile.
# 6  
Old 10-03-2012
a) There is a limit to how much stuff you can cram in a single shell variable. On some systems, this can be as small as a screenful or so, so don't go overboard.

b) If you're doing awk | awk | sed | cut | kitchen | sink, you probably could have done that all in one awk and saved yourself a lot of bother and CPU time. You don't need to use awk twice here:

Code:
awk '{ sum += $2 } END { print sum }' inputfile

c) That's also a useless use of cat, since awk does not need cat's help to read a file. You can use it as shown above, listing the filename on the end.

To answer question 2, there's actually a few ways to set more than one variable at once. read is a particularly flexible way of doing so. It helps if you know how many are coming, though, or can convince certain ones to come first. Everything you didn't list will get crammed into the last variable you list.

Code:
read SUM NUMBERS <<EOF
`awk '{ sum += $2; A[++L]=$0 } END { print sum ; for(N=1; N<=L; N++) print A[N]}' inputfile`
EOF

I saved all the numbers in the awk array A so I could print them later. This lets me print the sum first, and then the rest, letting me pick the sum out from the front with read.

You could also do
Code:
set -- `awk '{ sum += $2; A[++L]=$0 } END { print sum ; for(N=1; N<=L; N++) print A[N]}' inputfile`

SUM=$1
shift
REST="$*"

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-03-2012
Thanks Corona688 and RudiC....really appreciate it. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #506
Difficulty: Easy
A flowchart is a visual representation of an algorithm or process consisting of boxes or 'nodes' connected by directed edges.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curious

I dont get something about sed If i have a text file inside contain a:a:a:a:a sed "s/"$title:$author:$price:$qtyAvailable:$qtySold"/"$Ntitle:$author:$price:$qtyAvailable:$qtySold"/" This work!! but If i have a text file inside contain Tom Tom:La La:Di Di :Do Do :De DE It cannot work... (2 Replies)
Discussion started by: GQiang
2 Replies

2. UNIX for Dummies Questions & Answers

Curious about the -9

I was talking to a coworker and we got into a discussion about the -9. No one knew where the -9 came from and it's not in the man. I suggested that it was like counting to 10 (0-9) and you finally get to the point that that's it, the durned thing is going to die. So how did the -9 come to mean... (3 Replies)
Discussion started by: pflickner
3 Replies

3. Solaris

Curious MPxIO problem

Hello folks, I have a newly installed Solaris 10 system running on a T6320 blade. I have set up LDM with the intent to move an ldom from another blade to this one. So far, so good. I had the SAN folks make the LUNs belonging to the ldom visible to my new blade and I can see them, all 4 paths.... (4 Replies)
Discussion started by: Ranck
4 Replies

4. Shell Programming and Scripting

curious

sorry, just simple question: how can i do this in bash> foreach i( 1 2 3 ) sed 's/Hello/Howdy/g' test$i > test$i.new mv test$i.new test$i end (6 Replies)
Discussion started by: kurosaki
6 Replies

5. UNIX for Advanced & Expert Users

Curious 'ls' Issue

Hi, I am seeing a curious issue with 'ls' command. If I open a telnet session of my Solaris box and give "ls". The output is in 3 columns. a b c d e f g h i j k l However, if I give the same command after a couple of hours in the same window, it goes to 6 columns according to the... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

6. Linux

Curious?

To correct most of the problems with this language, How do I remove the DOS and WORD stuff from it? These come from the fact that it was written on those with a Microsoft supplied platform at the writers request. (1 Reply)
Discussion started by: River Freight
1 Replies

7. UNIX for Dummies Questions & Answers

Curious Dummy

I have a website but I do not for the life of me know how to upload using unix based command lines. Can someone send me a good site that has these commands. That and I am curious to know more about command line based interfacing. :D Curious Dummy (1 Reply)
Discussion started by: highway39
1 Replies

8. UNIX for Dummies Questions & Answers

Curious

Dear All I am curious to know, that in a system compromise, when someone has access to a box, does that individual have access to a shell on the system, i.e. the person is logging into the system using telnet or SSH to remotely access the box?? How does this individual/ hacker access the system. ... (2 Replies)
Discussion started by: skotapal
2 Replies

9. UNIX for Dummies Questions & Answers

Just curious, does Unix stand for anything?

If not, where did the name come from? (6 Replies)
Discussion started by: pudad
6 Replies

Featured Tech Videos