Using the part of a line as a variable?

Hello Friends,

I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD.

When I type

MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/

it must do the following job

cd /homefolder/hhk/ADS/

I just want to use the part of line strarting from /homefolder...

Can anyone tell me how to do ?

Thanks

alias mycmd='perl -wle "$ARGV[0]=~/\@.+?(\/.+)/;print qq(cd $1)"'

hmm sad but 'alias' can't understand that line..
mycmd fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ not working for some reason

1 Like

Thank you very much tip78.

When I use the command mycmd like that

mycmd fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/
it would do
cd /homefolder/hhk/ADS/

but it couldn't cange my directory to /homefolder/hhk/ADS/ :frowning:

try this:

alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;`cd $1`"'

or that:

alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;chdir qq($1)"'
1 Like

sorry both of them don't work again.

thank you for your efforts

well..

alias mycmd='perl -we "$ARGV[0]=~/\@.+?(\/.+)/;system qq(cd $1)"'

???

whatever
make a script 'mycmd' and put it in your /usr/sbin

#!/usr/bin/perl -w

chdir"$1" if($ARGV[0]=~/\@.+?(\/.+)/);

if it's not work again try to change 'chdir "$1"' for 'system "cd $1"' or `cd $1`

but FIRST don't forget to make 'unalias mycmd'

1 Like

it gives the error following

Can't exec "cd": No such file or directory at -e line 1.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:25 PM ----------

ok now trying to put it my bin file

---------- Post updated at 02:37 PM ---------- Previous update was at 02:27 PM ----------

for all of three there are some syntax errors :frowning:

---------- Post updated at 02:47 PM ---------- Previous update was at 02:37 PM ----------

for all of three there are still some syntax errors

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 :frowning:

1 Like

I have some ideas but have no script knowledge :slight_smile:

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 ?

nah. even

alias mycmd='perl -e "print qq(/etc)"|cd'

not working
all you can have: mycmd your_parameters|cd

so final variant:

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

both the final variants didn't work. sorry. thank you very much for your effort.

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 :frowning:

1 Like

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.

#!/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:

copy_from agama@charlie:/tmp/foo/bar
1 Like

right

rpf, this will be working:

#!/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/

1 Like

You may want to try the aliases...

root@bt:~# cat ~/.aliases 
#!/bin/bash
MYCMD()
{
    cmd=$(echo $1 | sed 's/.*@[a-z.]*//g' )
    cd $cmd
}
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

1 Like

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

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

then try this :wink:

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

Try this...

#!/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 ----------

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

--ahamed

There's a lot more for all of us :slight_smile: