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


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-17-2017   -   Original Discussion by Loc
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   -   Original Discussion by Loc
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 20 November 2017, 2:37 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 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   -   Original Discussion by Loc
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   -   Original Discussion by Loc
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 November 2017, 9:25 PM EST
Location: Saskatchewan
Posts: 22,512
Thanks: 1,151
Thanked 4,273 Times in 3,946 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   -   Original Discussion by Loc
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 November 2017, 9:29 AM EST
Location: Aachen, Germany
Posts: 11,625
Thanks: 320
Thanked 3,617 Times in 3,322 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   -   Original Discussion by Loc
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 17 November 2017, 12:40 AM EST
Posts: 179
Thanks: 26
Thanked 38 Times in 33 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   -   Original Discussion by Loc
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 November 2017, 12:28 AM EST
Location: San Jose, CA, USA
Posts: 10,661
Thanks: 572
Thanked 3,734 Times in 3,186 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
Closed

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 04:36 PM
Shell assign variable to another variable 3junior Shell Programming and Scripting 3 11-18-2010 11:20 PM
assign awk's variable to shell script's variable? tiger2000 Shell Programming and Scripting 4 04-13-2010 04:53 PM
Assign this to a variable.... ak835 Shell Programming and Scripting 5 10-29-2009 09:03 AM
assign a value to a variable Shivdatta Shell Programming and Scripting 10 01-17-2007 07:40 AM



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