A better way to assign values to variables - shell
so i've been used to doing it this way:
I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont want to have to evoke two commands just to assign a variable. the two commands in this case being "echo" and "awk".
also, i'm hoping the proposed solutions can be used on all unix systems.
I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont want to have to evoke two commands just to assign a variable. the two commands in this case being "echo" and "awk".
also, i'm hoping the proposed solutions can be used on all unix systems.
The shell i will be using in most cases is:
What OS are you using? /bin/sh varies considerably from system to system. (It is a 1980's vintage Bourne shell on some systems, a hard link or a symlink to a Korn shell on some systems, a hard link or symliink to bash on some systems, and there are lots of other possibilities on other systems.)
And, 4val is not a valid variable name (at least in Bourne, bash, and Korn shells where user defined shell variables start with an underscore or an alphabetic character; not a number).
I don't understand why you're using echo and awk to get the 2nd and 4th fields from constant strings. If the strings are constants, why not just use:
So, if you aren't processing constant strings, where are they coming from? Are they already stored in a shell variable? Are they from the 1st line of a file?
Arrays can't be used on all UNIX systems with /bin/sh, but we can probably come up with something portable and efficient if you give us a better description of where the data comes from and what you want to do with it.
Last edited by Scrutinizer; 09-06-2014 at 03:42 PM..
What OS are you using? /bin/sh varies considerably from system to system. (It is a 1980's vintage Bourne shell on some systems, a hard link or a symlink to a Korn shell on some systems, a hard link or symliink to bash on some systems, and there are lots of other possibilities on other systems.)
And, 4val is not a valid variable name (at least in Bourne, bash, and Korn shells where user defined shell variables start with an underscore or an alphabetic character; not a number).
I don't understand why you're using echo and awk to get the 2nd and 4th fields from constant strings. If the strings are constants, why not just use:
[CODE]VAL2=3[
VAL4=3/CODE]
So, if you aren't processing constant strings, where are they coming from? Are they already stored in a shell variable? Are they from the 1st line of a file?
Arrays can't be used on all UNIX systems with /bin/sh, but we can probably come up with something portable and efficient if you give us a better description of where the data comes from and what you want to do with it.
the os i'm working on varies, some are linux, aix, sunos, hpux.
the examples i gave, are really just examples.
so in the case of:
please note, that the "7 3 2 38 3" are values from a different process which must be assigned. they are really not constants. they change.
looking for something like this that assigns by arrays?
this is just an example. im trying to grab whatever is in 4th field of another variable and assigninng it to a new variable?
Is this what you are after?
OSX 10.7.5, default bash terminal, manually...
yes. something like this. but how do u specify the delimiter if it is something other than space?
maybe awk can be used here? the goal here is to be able to do this without calling two commands.
If you're going to use /bin/sh on Solaris/SunOS systems, you have to restrict yourself to features available in a pure 1980s Bourne shell (no $(cmd) command substitution, no shell variable arrays, no ${var#pattern}, ${var##pattern}, ${var%pattern}, ${var%%pattern}, nor ${var:start:length} variable expansions, ...).
For what you have described you could use the following with any shell I've ever seen that would be installed as /bin/sh on any Linux or UNIX system:
and would produce the output:
Note that the here-document in this script has two tab characters before the $set and one tab before the EOF. If you change the tab before the EOF to spaces this script will not work!
This User Gave Thanks to Don Cragun For This Post:
i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea.
but, i want to be able to assign each of the value returned by this program to a variable.
in the latest instance, the program gave the following 6 values:
4... (8 Replies)
I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables.
I need to read this file which is an input to my script
Config.txt
file name, first path, second... (7 Replies)
I am trying to read a input file which has two columns separated by space
Input file
server1 server2
server3 server4
server5 server6
When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values
while read a b
do
echo "$a"
echo... (5 Replies)
Hi,
I have requirement to assign values to variables which are created dynamically.
Below is the code which i am using to achieve above requirement.
#!/bin/ksh
oIFS="$IFS"; IFS=','
STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3"
set -A... (1 Reply)
For example, I have a file with below lines containing VOB tags and VOB paths.
* /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB
* /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB
* /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB
*... (4 Replies)
Hi,
I have a file like the following...
CUST=
DIR=
NULIST=
name=philps_123
How can i add values to each of these unassigned variables using a shell script?
say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
The text file has one single row and looks like this
Q1 P1 2006
I have to pick up this values from a shell script into three different variables,
say quarter, period and year from the above text file. Some one know's how to do this? I went through 'sed', dint really know how to... (3 Replies)
Hello,
I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables.
my cat.dat is:
11
12
+380486461001
12
13
+380486461002
13
14
+380486461003
i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
hey people..
i have a configuration file that looks like
7080 7988
net04.xxxxx.edu
20
where 20 is the number of threads in the thread pool initially.
net04.xxxxx.edu is the hostname.
and 7080 7988
are two ports. first one for client requests and second one for dns communication. now my... (2 Replies)