Visit The New, Modern Unix Linux Community


Problems assigning a string to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems assigning a string to a variable
# 1  
Problems assigning a string to a variable

Hello everyone,

My problem looks quite simple , how to assign a string with spaces and lines "\n" to a variable.

I've tried all kind of quoting and is impossible, bash always try to execute the string and never executes a simple assignation.

This is part of the code
Code:
f_add_host_register () {
  
  v_data="$2"
  v_host="$1"
  
  # We compose a register in a local variable
  
  v_tmp_${v_host}="`echo $v_data |
  awk 'BEGIN { FS=";" }
         { print $4 "() {"
           print "    SYSTYPE=" $2
           print "    SYSSERIAL=" $3
           print "    HOSTNAME=" $4
           print "    IPADDRESS_1=" $5
           print "    IFAZ_1=" $6
           print "    LINUXIFAZ_1=" $7
           print "    PORT_1=" $8
           print "    LANNAME_1=" $9
           print "    MAC_1=" $10
           print "    STRING=" $11
           print "    REDHATSERIAL=" $12
           print "    KEYBOARD=" $13
           print "    SUPPLIER=" $14 
           print "}"}'`"
   
  #echo $v_tmp_${v_host}
}

And this is the erroneous output:

Code:
./read_csv_file.sh: line 70: v_tmp_sat01=sat01() {
    SYSTYPE=-
    SYSSERIAL=CZC804385F
    HOSTNAME=sat01
    IPADDRESS_1=10.70.45.1
    IFAZ_1=NIC1
    LINUXIFAZ_1=eth0
    PORT_1=0
    LANNAME_1=iMAS0
    MAC_1=00###882294
    STRING=1
    REDHATSERIAL=
    KEYBOARD=uk
    SUPPLIER=hp
}: command not found
./read_csv_file.sh: line 73: v_tmp_nats01=: command not found
./read_csv_file.sh: line 70: v_tmp_nats02=nats02() {
    SYSTYPE=-
.................


I guess I ve done this before but how?!?! i dont remember ,
It's runnig over bash in a redhat 5.3 SE

Help me plz.

Thx

---------- Post updated at 11:21 AM ---------- Previous update was at 09:41 AM ----------

Thousand of test before. i do it in 2 steps.

Code:
  v_tmp=`echo $v_data |\
  awk 'BEGIN { FS=";" }
         { print $4 "() {"
           print "    SYSTYPE=" $2
           print "    SYSSERIAL=" $3
           print "    HOSTNAME=" $4
           print "    IPADDRESS_1=" $5
           print "    IFAZ_1=" $6
           print "    LINUXIFAZ_1=" $7
           print "    PORT_1=" $8
           print "    LANNAME_1=" $9
           print "    MAC_1=" $10
           print "    STRING=" $11
           print "    REDHATSERIAL=" $12
           print "    KEYBOARD=" $13
           print "    SUPPLIER=" $14 
           print "}"}'`
  
  # Here we save data for a specific host to a variable wich name 
  # contains the name of the host
  
  # THIS IS THE LINE
  eval "v_tmp_"$v_host="\$v_tmp"


Last edited by trutoman; 09-27-2010 at 06:22 AM.. Reason: resolved
# 2  
use as below,

v_data=$2
v_host=$1

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #875
Difficulty: Medium
The primary mission of UNIX.com is to be a shell script writing forum.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015. Am trying something like below but it is not assigning Please let me know if am missing something. Thanks in advance. ################################ #!/usr/bin/ksh DT=06302015 ... (7 Replies)
Discussion started by: weknowd
7 Replies

2. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

3. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

4. Shell Programming and Scripting

Script stops running after assigning empty string for a variable

Hi, This is the first time I see something like this, and I don't why it happens. Please give me some help. I am really appreciate it. Basically I am trying to remove all empty lines of an input.. #!/bin/bash set -e set -x str1=`echo -e "\nhaha" | grep -v ^$` #str2=`echo -e "\n" |... (4 Replies)
Discussion started by: yoyomano
4 Replies

5. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

8. Shell Programming and Scripting

assigning a variable

hi all, in ksh, how do i assign the output of a find command to a variable, e.g am trying something like this : totalNoFiles=$(print find ./ -name "SystemOut*.log"); but when i echo $totalNoFiles it displays find ./ -name "SystemOut*.log" instead of the total number of... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies

10. Shell Programming and Scripting

variable= 'cat file|wc -l' String problems

Hi, does anybody knows about wc -l, how to transform it inot a just number? this script ALWAYS executes the command3!!, However, the value of BMU_RUNNING is 1 case $BMU_RUNNING in *0) command1 ;; *1) command 2;; *)command 3;; esac The... (3 Replies)
Discussion started by: Santiago
3 Replies

Featured Tech Videos