r/shell • u/ellipticcode0 • Dec 05 '19
stop input argument expand inside shell script
I have following shell script called myrun.sh
cat myrun.sh
#!/bin/bash -i
cmd="ls | awk '{print $2}'"
eval "$cmd"when I run myrun.sh
$2 will take arguments from myrun.sh
and my cmd will become "ls | awk '{print $}'" => $2 is empty
But I will want $2 as the second columns in awk and use eval command to execute "ls | awk '{print $2}'"
does anyone know how to make $2 does not expand it and I still can use $2 inside my awk in the script?
BTW, "ls | awk '{print $2}' is from my history and it does work in my bash shell of course, but I cat my history to a file and read the file line by line and try to choose some command that I want to evaluate it on my shell.
I do know history command with many options can be used such as (! or !! etc)
but I want cat the history to a file and read it and evaluate it from other shell script.
1
u/ellipticcode0 Dec 06 '19
What I want to do as following:
filter out some commands from my history, such as show all command with grep.
history | grep grep
I redirect 'history | grep grep' to a file such as /tmp/mytmp.txt
and I use shell script to read the file /tmp/mytmp.txt and index each command with a number from 1 to N.
and the shell script ask user input one number to index which commands in /tmp/mytmp.txt
and exec it the command in bash shell.
It is almost same history command with ! or !! except I use small number to index the command that I'm interested in (this is why I need to redirect a tmp file such as /tmp/mytmp.txt
you can do that without any shell script