0

I would like to issue a command available in my current shell (where I am running my bash script from). As it is right now if I put the uecap command in my bash script as it is. The script fails with reason uecap command not found.

Although if I issue the uecap command directly from my current shell it works fine.

My current shell is on a separate process with it is own process id.

here is my bash shell as an example:

 #!/bin/sh
 while read i
 do 
 cid=`echo "$i"  | cut -b1`
 rid=`echo "$i"  | cut -b9-18`
 rm -rf $1_TEMP
 uecap -r rid -c cid
 done < $1ID.log

The way I run the bash script is by issuing this command:

!./bashscript $node.

On the same not, is there a way to run a command using another process from your bash script?

nader
  • 141
  • 2
  • 13
  • what is `uecap` and where does it reside? – bansi Jan 23 '16 at 03:10
  • It resides in another process. It is a special command related to another shell process. I run the bash script from that shell . Not sure if this would resolve ambiguity but think of it as telnet into a router then running the bash script from there. I could run the bash script (residing in the server) but I am not sure how to utilize the telnet commands in my bash script. – nader Jan 23 '16 at 03:12
  • if you mean to run command as another user you can use [sudo](http://linux.die.net/man/8/sudo) – bansi Jan 23 '16 at 03:12
  • Hi bansi hope this clarifies what I am trying to do – nader Jan 23 '16 at 03:16
  • still don't know the exact scenario. for automating any interactive programs you can use [expect](http://linux.die.net/man/1/expect). Check this [SO solution](http://stackoverflow.com/questions/7013137/automating-telnet-session-using-bash-scripts) for automating tenlet. – bansi Jan 23 '16 at 03:22
  • In my situation I already teletted to the host. I am running my bash script from there and I want to use some telnet specific commands in my bash script. – nader Jan 23 '16 at 03:28
  • I think I am understanding your situation somewhat (may be wrong). you telnet to another system ( your router) and run some command (uecap) which resides in the machine from where (your comp) you connected and send its output to the destination machine (router). is that so? – bansi Jan 23 '16 at 03:35
  • You say '`uecap` resides in another process'; that's puzzling — it isn't a process, but … what is it? An alias? Your context is completely unclear, so it is impossible to provide a useful answer. Please go back to the drawing board (temporarily delete the question), and rewrite it carefully, explaining more precisely what `uecap` is and why it can't be run from your shell script but can be run from the other script/process (and preferably what it means to run it, or communicate with it, when it is running in the other process). – Jonathan Leffler Jan 23 '16 at 03:39
  • 1
    Yes the uecap command is an alias. – nader Jan 23 '16 at 03:52

1 Answers1

1

Likely you've just answered your own question. Since uecap is an alias, then it's not exported to the processes issued by your interactive shell, and you can't force them shell to export it, for aliases there's no export command.

What you can do is to create a file containing that alias definition and then source than file from within your script, something like this:

alias uecap > ~/tmp/uecap_alias

and within a shell script

source ~/tmp/uecap_alias
...
uecap -r rid -c cid

you may also try to pass uecap alias definition through environment variables, something like

UECAP_ALIAS="$(alias uecap)" ./your_shell_script

and within the script

eval "$UECAP_ALIAS"
...
uecap ...

but beware that eval "$VAR" is quite fragile on whitespaces, quoting and weird symbols, so it should be used with extra care and only when you're 100% sure about what you're going to eval (think of SQL injections and alike).

user3159253
  • 16,836
  • 3
  • 30
  • 56