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 1 Week Ago
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 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 29 May 2017, 7:25 AM EDT
Location: Chennai
Posts: 2,477
Thanks: 553
Thanked 1,172 Times in 1,055 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 1 Week Ago
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 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 May 2017, 4:17 PM EDT
Location: Saskatchewan
Posts: 22,183
Thanks: 1,092
Thanked 4,174 Times in 3,861 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 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2017, 6:37 AM EDT
Location: Aachen, Germany
Posts: 10,830
Thanks: 272
Thanked 3,324 Times in 3,061 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 (1 Week Ago)
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 29 May 2017, 5:57 AM EDT
Posts: 162
Thanks: 22
Thanked 29 Times in 25 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 1 Week Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 29 May 2017, 4:14 AM EDT
Location: San Jose, CA, USA
Posts: 10,309
Thanks: 521
Thanked 3,587 Times in 3,055 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 (1 Week Ago)
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 07:29 AM.