Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Assign expression to a variable

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-17-2017
Loc Loc is offline
Registered User
 
Join Date: Feb 2017
Last Activity: 17 May 2017, 1:06 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Assign expression to a variable

This code strips out any []. It works great

Code:
echo "127[.]0[.]0[.]1" | tr -d "[]"

I would like to do the same thing but with shell scripting. User would enter:

Code:
./test 127[.]0[.]0[.]1

Output should be: 127.0.0.1
I would like to assign it to a different variable. I have something like this but I get a syntax error and I'm not sure why.

Code:
#!/bin/bash
a=$1 | tr -d "[]"
echo $a

This works just fine:

Code:
echo $1 | tr -d "[]"

But I would like to assign it to a variable and I don't know how.
Sponsored Links
    #2  
Old Unix and Linux 05-17-2017
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 15 September 2017, 12:22 AM EDT
Location: Chennai
Posts: 2,606
Thanks: 571
Thanked 1,238 Times in 1,116 Posts
Hello Loc,

Could you please try following and let me know if this helps you.

Code:
cat script.ksh
VAR=$(echo $1 | tr -d "[]")
echo $VAR

So following will be output while running it.

Code:
./script.ksh 127[.]0[.]0[.]1
127.0.0.1

Thanks,
R. Singh
Sponsored Links
    #3  
Old Unix and Linux 05-17-2017
Loc Loc is offline
Registered User
 
Join Date: Feb 2017
Last Activity: 17 May 2017, 1:06 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
I thought it would display the output twice because you used echo twice but it's not the case. This works great. thank you
    #4  
Old Unix and Linux 05-17-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 September 2017, 5:41 PM EDT
Location: Saskatchewan
Posts: 22,391
Thanks: 1,126
Thanked 4,230 Times in 3,911 Posts
Quote:
Originally Posted by Loc View Post
I thought it would display the output twice because you used echo twice but it's not the case. This works great. thank you
That's what the $( ... ) brackets do, they capture output so you can assign it to something.
Sponsored Links
    #5  
Old Unix and Linux 05-17-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 3:24 AM EDT
Location: Aachen, Germany
Posts: 11,346
Thanks: 297
Thanked 3,521 Times in 3,240 Posts
How about pure shell (Parameter Expansion : Pattern substitution):


Code:
echo $1, ${1//[][]}
127[.]0[.]0[.]1, 127.0.0.1

The Following User Says Thank You to RudiC For This Useful Post:
RavinderSingh13 (05-19-2017)
Sponsored Links
    #6  
Old Unix and Linux 05-18-2017
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 12 September 2017, 2:10 AM EDT
Posts: 171
Thanks: 24
Thanked 31 Times in 27 Posts
I guess you mean


Code:
./script.ksh '127[.]0[.]0[.]1'

instead of


Code:
./script.ksh 127[.]0[.]0[.]1

because without the quotes, your script would not see the brackets.
Sponsored Links
    #7  
Old Unix and Linux 05-18-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 2:57 AM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
Quote:
Originally Posted by rovf View Post
I guess you mean


Code:
./script.ksh '127[.]0[.]0[.]1'

instead of


Code:
./script.ksh 127[.]0[.]0[.]1

because without the quotes, your script would not see the brackets.
Hi rovf,
If, and only if, there is a file named 127.0.0.1 in the directory in which you invoke the command:

Code:
./script.ksh 127[.]0[.]0[.]1

then the shell would start script.ksh with $1 set to 127.0.0.1. Otherwise, the shell would start script.ksh with $1 set to 127[.]0[.]0[.]1.

With the specific code suggested in this thread in posts #2 and #5, the output produced would be identical if the input operand is single-quoted, double-quoted, backslash-escaped, or unquoted.
The Following User Says Thank You to Don Cragun For This Useful Post:
rovf (05-19-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assign regular expression change to only first two columns quincyjones Shell Programming and Scripting 7 12-20-2010 03:36 PM
Shell assign variable to another variable 3junior Shell Programming and Scripting 3 11-18-2010 10:20 PM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 03:53 PM
Assign this to a variable.... ak835 Shell Programming and Scripting 5 10-29-2009 08:03 AM
assign a value to a variable Shivdatta Shell Programming and Scripting 10 01-17-2007 06:40 AM



All times are GMT -4. The time now is 03:31 AM.