I am pretty new to Unix. Trying to pick up some slack while a coworker is out on vacation.
Basically the script is working fine however when I go through the testing phase and have to make mods it is a pita.
Here is an example of what I have
That is a small piece. There are several other areas alot more complex. There are 36 csv files that we are loading into a reporting website every quarter. Each variable ties to a file name. That being said we have variable names from src_file_1 through src_file_36. My thought is that I could generate a loop that would change the variable name with each cycle.
Thanks in advance for any insight.
---------- Post updated at 09:24 AM ---------- Previous update was at 07:28 AM ----------
...
so why when I manually do the eval it comes up with the right text, but when assigning the eval to a variable and then echo it it comes out wierd?
I am unable to reproduce your test in my system. This looks like the output of some shell script. I'll need to look at the script to figure out what's going on.
However, what's happening at the end is that the value of the variable "src_file_name" is the following two values appended to each other in the order shown:
(1) the value of $$
(2) the constant text "{src_file}"
Now, $$ is a built-in shell variable which holds the value of the PID, the process id. You can check the PID of each process in your shell by executing the following command and looking at the value in the 2nd column:
You can also check the value of your current shell PID:
And view it and its descendent processes in the process list:
As for your problem, you are creating variable names within the loop, and you want to access their values for your file names. There is a construct "${!variable}" for that.
A simple test follows. I create and assign a variable and echo its value:
Now, I assign the text "pointer_to_val" to another variable and echo it:
So far, everything is as expected.
But in the last echo command above, let's say I want to see the value of pointer_to_val, i.e. I want to see the value 123456 that pointer_to_val points to.
To do that, I'd use ${!variable} construct:
This idea could be incorporated in your script as follows:
And if the ${!variable} construct is not supported in your shell, you could use the old-fashioned eval construct:
I ended up getting it working. I wish the exclamation point piece of code would work on our box. Would have saved me hours of searching on the net.
This is wheat I ended up with. Might be a little crude but it works.
I'm glad it is working for you, but I'm surprised that the line I marked in red is working. It looks like you intended to execute test with the three operands trying to determine if two filenames were the same. But:
you are missing the test or [ and ] that would be required to make it a test command, and
when comparing strings, you should use = (or == if you were using [[ and ]] instead of test) instead of -eq.
The way you have this if statement written, it is executing the 1st filename with the two arguments -eq and the 2nd filename. If that returns a zero exit status, the then branch is executed; otherwise the else branch is executed.
If the intent was to use one branch the 1st time through the loop, why not test for the value of i being 1 instead of comparing filenames? You might consider changing:
close to the end of your script to:
This User Gave Thanks to Don Cragun For This Post:
Hello, please assist:
users="test1 test2"
keytest1="abcd"
keytest2="dbcd"
for i in $users
do
echo "$key${i}" > fileout
done
So, my objective is to take the current user (ie test1) in loop and echo its associated keyname (ie keytest1) variable to a file.
The echo... (2 Replies)
Hi,
I have a out from a command i need to grep a report. For that i need loop 3 variable for that. How i can loop need help.
Symmetrix ID : 123456
Masking View Name : Host16
Last updated at : 04:13:06 PM on Thu Mar 17,2011
Initiator Group Name : Host16
Host... (3 Replies)
i am having a file contants as below
my requirement is
for file in `awk -F "," '{print $8,$9}'` <temp.txt
echo "$file"
echo "$file">test.txt
a=`awk -F "," '{print $1}' `<test.txt
b=`awk -F "," '{print $2}' `<test.txt
but script reads , i want both the vales for further... (5 Replies)
Greetings. I'm completely new to shell scripting and quickly trying to catch on. Here's my scenario:
I have a text file, named ip.txt, containing IP addresses. I want to automatically perform a whois query on each address in the file, search the output for the country, and then put both the IP... (4 Replies)
Hi,
I need help on for loop need to add domain and IP
In domain list
1.com
2.com
3.com
In Ip list
1.1.0.1
1.2.0.1
1.3.0.1
1.com 1.1.0.1
2.com 1.2.0.1
3.com 1.3.0.1
I need to excute this command (4 Replies)
Can we assign two variables in a for loop?
I have an input file:
000301|20100502
835101|20100502
I want to read this file in a for loop and assign values to two different variables.
I did this now but did not work
for STORE,RUNDATE in `awk -F\| '{print $1,$2}' inputfile
... (4 Replies)
Hi, say I have a simple sh script like this:
for i in a b c d
do
for j in 1 2 3 4
do
echo "$i $j"
done
done
and the output is
a 1
a 2
a 3
a 4
b 1 (20 Replies)
I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends.
As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it.
To make a long story short around 20K pieces of... (2 Replies)
Hi Guys,
I have a scripts that uses a while loop to read a file and set 2 variables.
How can I do this so the variables can be used outside the while loop ?
Below is an example....# ./junk2 -m -e user
EXE=user master=TRUE
DB_TAG=PRODUCT
In loop MST=MST=testsvr1:3110
In loop ARGS=... (2 Replies)