Assign a sting to a name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign a sting to a name?
# 1  
Old 04-28-2009
Assign a sting to a name?

i want to assign a anme for a string, i got the things below, but it doesn't work.

MParc1=``

if [ "${num}" = 0 ]
then
$MPrac1=`Prac1` # assign $MPrac1 to Prac1
else
$MPrac1=`` #assigne $MPrac1 to nothing
fi
echo "${MPrac1} >>file

Output: (if num is 0)
Prac1

can you please find out the problem?
# 2  
Old 04-28-2009
First if you are posting try and put your code inbetween code tags.

You are using back ticks instead of quotes which is the problem. back tics try and run a remote command.

Try this :-

Code:
MParc1=""
      	
if [ "${num}" = 0 ]
	then
		$MPrac1="Prac1" # assign $MPrac1 to Prac1
	else
		$MPrac1=""         #assigne $MPrac1 to nothing
fi
echo "${MPrac1}" >>file

# 3  
Old 04-28-2009
Quote:
Originally Posted by lavascript
First if you are posting try and put your code inbetween code tags.

You are using back ticks instead of quotes which is the problem. back tics try and run a remote command.

Try this :-

Code:
MParc1=""
      	
if [ "${num}" = 0 ]
	then
		$MPrac1="Prac1" # assign $MPrac1 to Prac1
	else
		$MPrac1=""         #assigne $MPrac1 to nothing
fi
echo "${MPrac1}" >>file

Code:
# no need to initialize, but.....
MParc1=''
      	
if [ "${num}" -eq 0 ]
	then
		MPrac1='Prac1' # assign Prac1 TO MPrac1
	else
		MPrac1=''         #assign nothing  to MPrac1
fi
echo "${MPrac1}" >>file

# 4  
Old 04-28-2009
Quote:
Originally Posted by lavascript
First if you are posting try and put your code inbetween code tags.

You are using back ticks instead of quotes which is the problem. back tics try and run a remote command.

Try this :-

Code:
MParc1=""
      	
if [ "${num}" = 0 ]
	then
		$MPrac1="Prac1" # assign $MPrac1 to Prac1
	else
		$MPrac1=""         #assigne $MPrac1 to nothing
fi
echo "${MPrac1}" >>file

it is not working
it show something like this
./c.sh: line 37: =: command not found
./c.sh: line 43: =: command not found
./c.sh: line 48: =Prac1: command not found
# 5  
Old 04-28-2009
Sorry i just copied your original code and forgot to remove the $ from the variable assignment.

See vgersh99's solution.

you could use either ' ' or " " in this instance.
# 6  
Old 04-28-2009
Quote:
Originally Posted by mingming88
it is not working
it show something like this
./c.sh: line 37: =: command not found
./c.sh: line 43: =: command not found
./c.sh: line 48: =Prac1: command not found

I assume you have put the correct interpreter at the top of the script and not just copied what we put in the post here?

I was just using your origiinal example and adapting the bits accordingly.

You may want to do this :-

Code:
#!/bin/ksh

MParc1=""
      	
if [ "${num}" = 0 ]
	then
		MPrac1="Prac1" # assign $MPrac1 to Prac1
	else
		MPrac1=""         #assigne $MPrac1 to nothing
fi
echo "${MPrac1}" >>file

But then don't forget i've not assigned $num anywhere.
You will also get the output of MPrac1 in file or file with nothing.
# 7  
Old 04-28-2009
It is working now.
Thank you guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk to search for sting in lines and then email.

I'm trying to use awk in a script to search for a string number in a plist file. basically its a plist that records the bandwidth usage, and if the number is greater than 3GB which is equals 3221225472 bytes, it will send me an alert by email. here is the file: =<?xml version="1.0"... (4 Replies)
Discussion started by: ivaldiz
4 Replies

2. Shell Programming and Scripting

Find and replace a string in group of sting

Hi, 1. I want to remove the line which contains "SUBPARTITION", but the condition is if line has ends with ")," replace with , else delete the line I was trying with the sed command i... (3 Replies)
Discussion started by: gavemani
3 Replies

3. Shell Programming and Scripting

Replace sting in a file

Hi Gurus, I need to search and replace string <! in a file with a blank. Some how my command sed -i 's/\<\!\ is not working .. Kindly help (1 Reply)
Discussion started by: r_t_1601
1 Replies

4. Shell Programming and Scripting

Inserting a string in another sting

Hi Experts, I need to insert a sting into another string at a specified position. Like the below. Regards, Tin-Tin (3 Replies)
Discussion started by: tinufarid
3 Replies

5. Shell Programming and Scripting

how to assign value

#! /bin/bash if ; then echo "Set number " else k=$1 sqlplus ${scheme}/${apsswd}@${server} @query.sql $k fi file query.sql looks like this select * from tab1 where number =${k}; =================================== it doesnt work my question is how to assign k value in last... (2 Replies)
Discussion started by: kvok
2 Replies

6. Shell Programming and Scripting

How to grep sting (*) ?

Hi all, I am trying to grep something from log file. For example. i need to grep sting = "*834*1" My LOG 2010-05-05 09:27:57,649|3|ABCD834|192.168.38.72|2365|444850623031|0|8|-|0|*834*1*1499413*00853662524#|470540.7219101273026475158 2010-05-05... (3 Replies)
Discussion started by: ooilinlove
3 Replies

7. Programming

Java sting up path problem

Hi java programmers I'm a newbee to the Java. Could please help me to set the path correctly. I'm getting the following error. ----jGRASP exec: javac -g C:\Documents and Settings\bogugk\Desktop\HelloWorldApp.java ----jGRASP wedge2 error: command "javac" not found. ---- This command... (1 Reply)
Discussion started by: repinementer
1 Replies

8. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

9. Shell Programming and Scripting

Extracting value from a sting using perl

Hi Experts, I want to extract the value of SESSIONID AND USERID from the this string using perl. XML Request },&quot;USER_PREF&quot;:{&quot;sys_lang&quot;:&quot;en-us&quot;,&quot;timezone&quot;:&quot;480&quot;,&quot;user_lang&quot;:&quot;en-us&quot;}}}</DEVICESIG></POLICYREQ>] The answer should be SessionID=1:3133050 and USERID=01451651 Please advise how... (3 Replies)
Discussion started by: namishtiwari
3 Replies

10. Shell Programming and Scripting

Verifying sting format in bash

Hello, This one has me stumped, although I suspect it might be easy. I have a file and need to split the entries into two varaibles. The format for a positive is 'AANNNN' where A is alpha and N is numeric. Thanks in advance (5 Replies)
Discussion started by: 98_1LE
5 Replies
Login or Register to Ask a Question