bash script: auto type command
I have a script that generate me some command that I must execute...
Example:
wolfy@wolfy-server:~$ ./myScript.sh
some linux command (Like: ls, cd. cp, ...)
wolfy@wolfy-server:~$
I would like that when I run myScript.sh it automaticaly type this command in my command line like:
wolfy@wolfy-server:~$ ./myScript.sh
wolfy@wolfy-server:~$ some linux command
so that I only need to check if is all ok with the command and press enter to run it... no copy neede开发者_C百科d :)
Can this be done?
Edit: I edited my example, so that everyone can see that this script can't be an alias, because this script return different commands each time. btw... the command is not my question, my question is how to put this "random" command in that place.
Closest you can come, IMHO is simply echoing the command (with proper quoting!!!!)
What you COULD do:
#!/bin/bash
alias smartls='ls -lah / | grep "ufv" | xargs cat'
Use it like
source ./mySript.sh
Tip: Instead of source ./myScript.sh
you can say the shorter . ./myScript.sh
Now the magic comes in when you type:
smartlsEscCtrl-e
With default (emacs) keybindings this will expand the command, leaving the command in the edit buffer for you to edit/review!
Tip: choose a non-intrusive but very short alias (like Q
, for me would do fine) and cut down on the number of keys to type
Tip: learn about mapping keys and bindings in bash and (probably) learn how to even map those three keys to a single key and be even quicker. Perhaps you can even include the sourcing of myScript.sh
in such a key binding.
HTH
It is definitely not possible to do this with a script.
You could write that command into your script and wait for a keypress...
If the script is generating the commands, why is it not running them?
If for some reason it has to be done in this way, then you can use 'eval' to run the commands:
eval $(./myScript.sh)
EDIT : Here is another attempt at this. This seems to working for all cases.
if [[ $# -ge 1 ]]
then
str=$1;
echo $str > myCmd
else
str=`cat myCmd`;
fi
echo $str
history -s $str
Execute this as : $ source myScript "cmd".(adding source is important). After that command is displayed on terminal and if you want to execute that enter !! or press the UP key (its the last command in the history), then you can also edit the command. Again, this script also stores your last command, so if you run this script without arguments it shows you the last command that you executed. Also, alias myscript="source myscript" can be done to make things easier.
Here is what the output looks like :
priyank@priyank-laptop ~ $ source try.sh 'cat i.cpp | grep main'
cat i.cpp | grep main
priyank@priyank-laptop ~ $ !!
cat i.cpp | grep main
int main()
priyank@priyank-laptop ~ $ . try.sh
cat i.cpp | grep main
priyank@priyank-laptop ~ $ !!
cat i.cpp | grep main
int main()
priyank@priyank-laptop ~ $
My first attempt : This is the best i could come up with, but still this has some problems (runs only basic cmds, no pipes etc) :
if [[ $# -ge 1 ]]
then
str=$1
echo $1 > myCmd
else
str=`cat myCmd`;
fi
echo $str | cat &
t=`cat`
$str;
Press Ctrl+D to run the command. Ctrl+C if you do not want to run the command. If there is something wrong you can copy and try the command. You can try and edit this script to suit your needs.
You can also run it as : bash script.sh "new cmd". New cmd will be overwritten in your myCmd file, which contains your command. If no cmd is provided then it uses the cmd in myCmd file.
Are you just looking for a script that asks you for confirmation before continuing? You can do that with echo and read:
#!/usr/bin/env bash
cmd="some command"
echo -n "press enter to execute: $cmd"
read
$cmd
You can even wrap that up into a general utility function:
function exec_with_confirm () { cmd="$@" echo -n "press enter to execute: $cmd " read $@ }
exec_with_confirm ls -l
exec_with_confirm du -sk .
This only goes so far, it won't work with shell meta-characters, to handle those you'd need to modify the function to take a string and pass it to bash -c, and send it quoted strings.
If your intent is just to have a confirmation before execution, this should solve that.
If your intent is to have it be placed on your next command line, then this won't do that.
精彩评论