Using the part of a line as a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using the part of a line as a variable?
# 15  
Old 10-30-2011
You may want to try the aliases...
Code:
root@bt:~# cat ~/.aliases 
#!/bin/bash
MYCMD()
{
    cmd=$(echo $1 | sed 's/.*@[a-z.]*//g' )
    cd $cmd
}

Code:
root@bt:~# MYCMD fish://mfong@vhl.gov.nd/root/Desktop/study
root@bt:~/Desktop/study#

It works just as a script sourced in the shell with functions and sourcing makes the function call available in the shell...

Normally, .aliases is sourced when you log in. i.e. you should/will find this piece of code . ~/.aliases in your ~/.profile or ~/.bashrc depending on your shell

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 16  
Old 10-31-2011
Hello ahamed. It is about to be done. We are very close. When I type

mycmd fish://jackboy@reacs1.ul.gov.tr/home_put1/jackboy

it gives

-bash: cd: 1.ul.gov.tr/home_put1/jackboy: No such file or directory

it must do

cd /home_put1/jackboy

(however your code does cd 1.ul.gov.tr/home_put1/jackboy)

how can it be fixed ?



thanks

Last edited by rpf; 10-31-2011 at 10:29 AM..
# 17  
Old 10-31-2011
Quote:
Originally Posted by rpf
Hello ahamed. It is about to be done. We are very close. When I type

mycmd fish://jackboy@reacs1.ul.gov.tr/home_put1/jackboy

it gives

-bash: cd: 1.ul.gov.tr/home_put1/jackboy: No such file or directory

it must do

cd /home_put1/jackboy

(however your code does cd 1.ul.gov.tr/home_put1/jackboy)

how can it be fixed ?



thanks

Code:
# unalias mycmd 
# mycmd () { cd $(echo "/${1#*//*/}") ; }

then try this Smilie
Code:
# mycmd fish://jackboy@reacs1.ul.gov.tr/home_put1/jackboy

# 18  
Old 10-31-2011
Try this...
Code:
#!/bin/bash
MYCMD()
{
    cmd=$(echo $1 | sed 's/.*@[a-z.0-9]*//g' )
    cd $cmd
}

Let me see if I can generalize it even more!...

--ahamed

---------- Post updated at 08:36 AM ---------- Previous update was at 08:35 AM ----------

Quote:
Originally Posted by ygemici
Code:
# unalias mycmd 
# mycmd () { cd $(echo "/${1#*//*/}") ; }

then try this Smilie
Code:
# mycmd fish://jackboy@reacs1.ul.gov.tr/home_put1/jackboy

I need to get my hands dirty on the string manipulations!... Smilie

--ahamed
# 19  
Old 10-31-2011
Quote:
Originally Posted by ahamed101
---------- Post updated at 08:36 AM ---------- Previous update was at 08:35 AM ----------



I need to get my hands dirty on the string manipulations!... Smilie

--ahamed
There's a lot more for all of us Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove part of the line from start of the line?

Hello, I am java command from a shell script which will generate the below output on the command prompt signature Base64 :... (10 Replies)
Discussion started by: chetanojha
10 Replies

2. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

3. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

4. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

5. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

6. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

7. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

8. Shell Programming and Scripting

Copy part of a variable

Hi, i was using a input file to get the last line of the file.But now i have stored the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable. previous code, cat input.txt <TIME>00:15:48</TIME>... (2 Replies)
Discussion started by: Shellslave
2 Replies

9. Shell Programming and Scripting

Repacing part of string with a variable

I have following strings in a file DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y What i need to do is replace the date 20070416 with anoth date which is stored in... (4 Replies)
Discussion started by: divz
4 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question