/bin/bash - variable substitution.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting /bin/bash - variable substitution.
# 1  
Old 04-21-2009
/bin/bash - variable substitution.

Is it possible with a bash variable to perform multiple substitution strings to one variable?

I have this variable:
echo $clock
TIMEZONE="US/Central"

What I would like to do with bash only it pull out just the "US" part of the variable.. which could be any number of countries.

this is where I am at:

echo ${clock:10}
US/Central"

echo ${clock:10#*/}
bash: testing: 10#*/: syntax error: operand expected (error token is "/")

echo ${clock#*/}
Central"


So, am I just missing something.. or can I only do one type of substition with a bash variable?

thanks,
Trey
# 2  
Old 04-21-2009
Try this:

Code:
echo $clock | sed 's!.*"\(.*\)/.*!\1!'

Regards
# 3  
Old 04-21-2009
Code:
colemar@deb:~$ echo ${clock:10:2}
US

colemar@deb:~$ a=${clock#*\"}
colemar@deb:~$ echo $a
US/Central"
colemar@deb:~$ echo ${a%%/*}
US


Last edited by colemar; 04-21-2009 at 05:49 PM.. Reason: %%
# 4  
Old 04-21-2009
Quote:
Originally Posted by Franklin52
Try this:

Code:
echo $clock | sed 's!.*"\(.*\)/.*!\1!'

Regards
thanks, but I was really just wondering if I could do this all with bash. Smilie

Quote:
Originally Posted by colemar
Code:
colemar@deb:~$ echo ${clock:10:2}
US

I know that, but the problem lies that US is not alway US thus it can be more then 2 characters long... Smilie
# 5  
Old 04-21-2009
Quote:
Originally Posted by trey85stang
I know that, but the problem lies that US is not alway US thus it can be more then 2 characters long... Smilie
Then you must do it in two steps, as outlined above.
I believe there is no way to do it at once with bash parameters substitution.
# 6  
Old 04-21-2009
Quote:
Originally Posted by colemar
I believe there is no way to do it at once with bash parameters substitution.
Seems to be possible, though ugly:
Code:
colemar@deb:~$ echo ${clock//@(*?=\"|\/*?)/}
US

# 7  
Old 04-21-2009
Quote:
Originally Posted by colemar
Seems to be possible, though ugly:
Code:
colemar@deb:~$ echo ${clock//@(*?=\"|\/*?)/}
US

looks like that will do it, good stuff. I appreciate it. This will save me a lot of work creating a bunch of variables in a script Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Bin/bash - xmessage very slow

Hello, I am showing the start of my script. I am finding that 'xmessage' is taking about 12-15 seconds to show. This in a terminal is very quick '/opt/vc/bin/vcgencmd get_camera'. Is there any way to get 'camera not detected' to show faster. Regards #!/bin/bash s=$(/opt/vc/bin/vcgencmd... (4 Replies)
Discussion started by: mad-hatter
4 Replies

3. Shell Programming and Scripting

Usage of #!/bin/sh vs #!/bin/bash shell scripts?

Some question about the usage of shell scripts: 1.) Are the commands of the base shell scripts a subset of bash commands? 2.) Assume I got a long, long script WITHOUT the first line. How can I find out if the script was originally designed für "sh" or "bash"? 3.) How can I check a given... (3 Replies)
Discussion started by: pstein
3 Replies

4. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

5. Shell Programming and Scripting

/bin/bash: Event not found.

Hi I'm new to scripting - please help me... I'm trying to run a script written by a friend: #!/bin/bash for aStat in .... do .... done when coping the script to the terminal I get: /bin/bash: Event not found. for: Command not found. (7 Replies)
Discussion started by: atira
7 Replies

6. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

BIN/BASH.

THANKS UNIX SYSTEM®.I was found my job from UNIX®.I USE MONKEY WRENCH WITH WARTER.I am now studying my studio with UNIX SYSTEM®. THANKS UNIX SYSTEM®. THANKS OUR OPEN GROUP. from Takayasu Sakashita.My name is Takayasu Sakashita. I respect you. Austin.PEACE!Bey bey. Your friend TAKA.Good... (1 Reply)
Discussion started by: administrator®
1 Replies

8. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

#!/bin/bash has stopped working

Hi I'm writing a script and I've put #!/bin/bash as the first line so that I can just type my scripts name 'whodate' at PS1 instead of ./whodate. This has suddenly stopped working for me. It used to be the case that I could start a script with #!/bin/bash and it would work, but for this script... (2 Replies)
Discussion started by: zorrokan
2 Replies
Login or Register to Ask a Question