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?
# 8  
Old 10-29-2011
thing is perl is changing directory with 'chdir' (it can be viewed by `pwd` command) but not actualy changing it in shell
have no idea what to do Smilie
This User Gave Thanks to For This Post:
tip78
# 9  
Old 10-29-2011
I have some ideas but have no script knowledge Smilie

do you know how to pick a part of a line ?

I think if we pick the part of line we can define it as a variable (say WAY) and use it in a script in my bin as

here the code for picking the part of line
cd $WAY


can we ?
# 10  
Old 10-29-2011
nah. even
Code:
alias mycmd='perl -e "print qq(/etc)"|cd'

not working
all you can have: mycmd your_parameters|cd

so final variant:
Code:
alias mycmd='perl -e "$ARGV[0]=~/\@.+?(\/.+)/;print qq($1)"'

This User Gave Thanks to For This Post:
tip78
# 11  
Old 10-29-2011
both the final variants didn't work. sorry. thank you very much for your effort.
# 12  
Old 10-29-2011
aha
the perl is printing out the required cutted result BUT cd can't deal with it
i've tryed lots of variants
my ideas are over
sorry Smilie
This User Gave Thanks to For This Post:
tip78
# 13  
Old 10-29-2011
What you are running into is a classic misunderstanding of current working directory in relation to a process. Your shell process has a working directory which is inherited by any process (command) that is started by the shell. The child process may change its current working directory, but that will not affect the shell's current working directory. If you think about it, it doesn't make any sense for a child process to mess with the parent's environment.

While the cd command doesn't look any different than a normal command, it is a shell 'built-in' which in other words is a directive to the shell causing the shell to change it's environment, and does not cause a child process to be created. This is how you are able to cause the current working directory to change in the shell.

If you are looking to perform some operation given a username@hostname:path, and have the results of the operation go into the directory on the local system using just path, then just write a script to do it. If you want the current working directory to be changed in the local shell after the command has executed, unless there is magic that I am unaware of, it cannot be done.

Here's an example of a script that will suss out the path, change the directory to the path, and then copy via scp all files from the matching path on the remote machine to the local host. It' may not be exactly what you are looking for, but it will give you an idea of how to go forward.

Code:
#!/usr/bin/env ksh
# assume $1 entered on the command line and is [user@]hostname:path

d=${1#*:}     # cut everything up through the first colon (:)
if ! cd ${d:-empty-path-given}
then
    echo "unable to switch to: ${d:-empty-path-given}"
    exit 1
fi

# copy from host:directory supplied on command line to current directory
scp $1/'*' .
exit $?

If you save the script in a file ($HOME/bin/copy_from for example), then this command will run the script and copy the files to the path /tmp/foo/bar:

Code:
copy_from agama@charlie:/tmp/foo/bar

This User Gave Thanks to agama For This Post:
# 14  
Old 10-30-2011
right

rpf, this will be working:
Code:
#!/usr/bin/perl -w

open(F,">/tmp/ftmp");
$ARGV[0]=~/\@.+?(\/.+)/;
print F "cd $1";
close F;
`sh /tmp/ftmp`;

name it 'mycmd' and put in /usr/local/sbin
then just: mycmd fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/
This User Gave Thanks to For This Post:
tip78
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