Parse file to .aliasrc - bash

I want to transform a string given in this form:
xyx some commands
into this form:
alias xyz="some commands"
I tried different combinations in the terminal. It seems (i'm not sure) that it worked once, but never when i run this from the script. I've read somewhere that this is a variable problem.
Alias for readability:
alias first="sed 's/\s.*//'"
alias rest="sed 's/\S*\s*//'"
cat f_in | tee -a >(one=$(first)) >(two=$(rest)) | tee >(awk '{print "alias "$1"=\""$2"\""}' > f_out )
I used awk in this way to parse "cat f_in" into "print". It doesn't work. Then, i used "awk -v" but it still doesn't work too. How to redirect variable $one and $two into awk:
{one=$(first) === first | read -r one }?

Is this what you're trying to do:
$ echo 'xyx some commands' |
awk '{var=$1; sub(/^[^[:space:]]+[[:space:]]+/,""); printf "alias %s=\"%s\"\n", var, $0}'
alias xyx="some commands"
$ echo 'xyx some commands' |
sed 's/\([^[:space:]]*\)[[:space:]]*\(.*\)/alias \1="\2"/'
alias xyx="some commands"

Related

shell script in a here-document used as input to ssh gives no result

I am piping a result of grep to AWK and using the result as a pattern for another grep inside EOF (not sure whats the terminology there), but the AWK gives me blank results. Below is part of the bash script that gave me issues.
ssh "$USER"#logs << EOF
zgrep $wgr $loc$env/app*$date* | awk -F":" '{print $5 "::" $7}' | awk -F"," '{print $1}' | sort | uniq | while read -r rid ; do
zgrep $rid $loc$env/app*$date*;
done
EOF
I am really drawing a blank here beacuse of no error and Im out of ideas.
Samples:
I am greping log files that looks like below:
app-server.log.2020010416.gz:2020-01-04 16:00:00,441 INFO [redacted] (redacted) [rid:12345::12345-12345-12345-12345-12345,...
I am interested in rid and I can grep that in logs again:
zgrep $rid $loc$env/app*$date*
loc, env and date are working properly, but they are outside of EOF.
The script as a whole connects to ssh and goes out properly but I am getting no result.
The immediate problem is that the dollar signs are evaluated by the local shell because you don't (and presumably cannot) quote the here document (because then $wqr and $loc etc will also not be expanded by the shell).
The quick fix is to backslash the dollar signs, but in addition, I see several opportunities to get rid of inelegant or wasteful constructs.
ssh "$USER"#logs << EOF
zgrep "$wgr" "$loc$env/app"*"$date"* |
awk -F":" '{v = \$5 "::" \$7; split(v, f, /,/); print f[1]}' |
sort -u | xargs -I {} zgrep {} "$loc$env"/app*"$date"*
EOF
If you want to add decorations around the final zgrep, probably revert to the while loop you had; but of course, you need to escape the dollar sign in that, too:
ssh "$USER"#logs << EOF
zgrep "$wgr" "$loc$env/app"*"$date"* |
awk -F":" '{v = \$5 "::" \$7; split(v, f, /,/); print f[1]}' |
sort -u |
while read -r rid; do
echo Dancing hampsters "\$rid" more dancing hampsters
zgrep "\$rid" "$loc$env"/app*"$date"*
done
EOF
Again, any unescaped dollar sign is evaluated by your local shell even before the ssh command starts executing.
Could you please try following. Fair warning I couldn't test it since lack of samples. By doing this approach we need not to escape things while doing ssh.
##Configure/define your shell variables(wgr, loc, env, date, rid) here.
printf -v var_wgr %q "$wgr"
printf -v var_loc %q "$loc"
printf -v var_env %q "$env"
printf -v var_date %q "$date"
ssh -T -p your_pass user#"$host" "bash -s $var_str" <<'EOF'
# retrieve it off the shell command line
zgrep "$var_wgr $var_loc$var_env/app*$var_date*" | awk -F":" '{print $5 "::" $7}' | awk -F"," '{print $1}' | sort | uniq | while read -r rid ; do
zgrep "$rid $var_loc$var_env/app*$date*";
done
EOF

Set a command to a variable in bash script problem

Trying to run a command as a variable but I am getting strange results
Expected result "1" :
grep -i nosuid /etc/fstab | grep -iq nfs
echo $?
1
Unexpected result as a variable command:
cmd="grep -i nosuid /etc/fstab | grep -iq nfs"
$cmd
echo $?
0
It seems it returns 0 as the command was correct not actual outcome. How to do this better ?
You can only execute exactly one command stored in a variable. The pipe is passed as an argument to the first grep.
Example
$ printArgs() { printf %s\\n "$#"; }
# Two commands. The 1st command has parameters "a" and "b".
# The 2nd command prints stdin from the first command.
$ printArgs a b | cat
a
b
$ cmd='printArgs a b | cat'
# Only one command with parameters "a", "b", "|", and "cat".
$ $cmd
a
b
|
cat
How to do this better?
Don't execute the command using variables.
Use a function.
$ cmd() { grep -i nosuid /etc/fstab | grep -iq nfs; }
$ cmd
$ echo $?
1
Solution to the actual problem
I see three options to your actual problem:
Use a DEBUG trap and the BASH_COMMAND variable inside the trap.
Enable bash's history feature for your script and use the hist command.
Use a function which takes a command string and executes it using eval.
Regarding your comment on the last approach: You only need one function. Something like
execAndLog() {
description="$1"
shift
if eval "$*"; then
info="PASSED: $description: $*"
passed+=("${FUNCNAME[1]}")
else
info="FAILED: $description: $*"
failed+=("${FUNCNAME[1]}")
done
}
You can use this function as follows
execAndLog 'Scanned system' 'grep -i nfs /etc/fstab | grep -iq noexec'
The first argument is the description for the log, the remaining arguments are the command to be executed.
using bash -x or set -x will allow you to see what bash executes:
> cmd="grep -i nosuid /etc/fstab | grep -iq nfs"
> set -x
> $cmd
+ grep -i nosuid /etc/fstab '|' grep -iq nfs
as you can see your pipe | is passed as an argument to the first grep command.

Bash capture a keystroke from another terminal

Using bash 4.3 and root powers, I want to be able to detect one keystroke (any key) from another terminal.
In terminal 1, a background process that writes to a named pipe once one character was read from another tty, say pts/18
read -rsn1 < /dev/pts/18 && echo > ./myfifo &
In terminal 2, an attempt to read a character from the same fifo
read -rsn1 < ./myfifo
It works moderately well, but only after several (3 or 4) keystrokes in pts/18, not the first one.
The mechanism appears to be redundant but it allows to start several background processes with different ttys and redirect to the same named pipe.
Hope you can help me.
One day I've tried to do that.
Wrote script who getting that you want with strace:
#!/bin/bash
# Couple of special symbols
BACKSPACE='\10\33[K'
BACKSPACE_EMPTY='\7'
BACKSLASH='\\'
QUOTE="\'"
DOUBLE_QUOUTE='\"'
LARROW='\10'
RARROW='\33[C'
#APPOSTRO="'\`'"
BACKSPACE_='\\33\[1P'
#LOGDATE=`date '+%Y/%m/%d %H:%M:%S'`
BADBIN='\\33\]'
while read -r line
do
# Avoiding binary symbols
NOBINline=$(echo "${line}" | strings)
# Current `pwd` value for session
CURRENT_PWD_OF_PID=$(readlink -f /proc/${1}/cwd)
# Getting username
USER_OF_PID=$(cat /proc/${1}/environ | strings | tr '.' ' ' | grep USER | awk -F= '{print $2}')
# Not the best but working way to create prompt line
HOSTNAME_OF_PID=`hostname -a`
STR_TO_REMOVE=$(printf "${USER_OF_PID}""#""${HOSTNAME_OF_PID}"":""${CURRENT_PWD_OF_PID}")
# Cut useless symbols from strace output.
parsed_line=$(echo "${NOBINline}" | perl -nale 'print $1 if ~/\"(.*)\"/gi')
if [ "${parsed_line}" == "\n" ]
then
parsed_line="{{ENTER}}"
fi
output_line=''
inchar_line=''
postinchar_line=''
inchar_line=$(printf "${parsed_line}")
if [ "${inchar_line}" == "{{ENTER}}" ]
then
echo ""
else
output_line=$(printf "${output_line}""${inchar_line}")
fi
if [ "${output_line}" != "${CURRENT_PWD_OF_PID}" -a "${output_line}" != "${STR_TO_REMOVE}" -a `echo "${NOBINline}" | grep -c "${BADBIN}"` -eq 0 ]
then
printf "${output_line}"
fi
done < <(sudo strace -e trace=write -s1000 -p $1 2>/dev/stdout)
How to use:
./script.sh <PID_of_mointored_console>
Also I've written the second script (much more ugly than first):
#!/bin/bash
while [ 1 ]
do
# lets and log_start -- names of two my scripts.
for PIDs in `ps awwux | grep [p]ts | grep bash | grep -v lets | grep -v log_start | grep -v grep | awk '{print $2}'`
do
PTS=$(ps awwux | grep "${PIDs}" | grep -v grep | awk '{print $7}')
if [ -f /tmp/bash_log.${PIDs} ]
then
sleep 1
else
touch /tmp/bash_log.${PIDs}
# lets.sh -- first script.
/bin/bash /root/lets.sh $PIDs >> /tmp/bash_log.${PIDs} &
fi
# tr -cd '\11\12\15\40-\176' < /tmp/x_bash_log.${PIDs} | sed 's/\]0;//g' | sed 's/\[C//g' > /tmp/bash_log.${PIDs}
done
for IFEMPTY in `find /tmp/ -type f -name 'bash_log*' -mmin +600`
do
if [ `cat "${IFEMPTY}" | grep -v "\'" | wc -c` -lt 2 ]
then
rm -rf "${IFEMPTY}"
else
sleep 1
fi
done
done
Start it in the background, place near first script.
It will catching all new terminal sessions, and start firstscript.sh <PID> for each, and creating /tmp/bash_log.<PID> file.
How to use:
./monitor.sh &
# Start new terminal
[sahaquiel#sahaquiel-PC ~]$ echo $$
916
[sahaquiel#sahaquiel-PC ~]$ HELO, IM A NEW STRING
bash: HELO,: command not found
[sahaquiel#sahaquiel-PC ~]$ tail -n3 /tmp/bash_log.916
916
''[sahaquiel#sahaquiel-PC ~]$ HELO, IM A NEW STRING
'''[sahaquiel#sahaquiel-PC ~]$ tail -n3 /tmp/bash_log.916
NB. I'm still working with it, created just for fun.
/tmp/bash_log files have awful formatting. I've also tried to log in the file backslashes, Ctrl+R, Ctrl+C and the same, but at now it's looks like not readable enough:
'''[sahaquiel#sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 123[BACKSPACE] [Backspace found. Rewritten string]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 12[BACKSPACE] [Backspace found. Rewritten string]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING 1[BACKSPACE] [Backspace found. Rewritten string]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ I WILL TYPE BACKSLASH THREE TIMES IN THE END OF THIS STRING
'''[sahaquiel#sahaquiel-PC ~]$ NOW I I[BACKSPACE] [Backspace found. Rewritten string]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIMES[LARROW] [Left arrow found.]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIME[LARROW]S[LARROW] [Left arrow found.]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TIM[LARROW]ES[LARROW] [Left arrow found.]: └────>
'''[sahaquiel#sahaquiel-PC ~]$ NOW I WILL PRESS \"LEFT ARROW\" THREE TI[LARROW]MES
You can change it somehow you want.
There are a lot of variables, I wanted to use them later for extended strings parse way, but it doesn't work now correctly.
P.S. If someone really interested in that, can check this project on my Github (check profile for link)

setting variables in bash script

I have the following command:
echo "exec [loc_ver].[dbo].[sp_RptEmpCheckInOutMissingTime_2]" |
/home/mdland_tool/common/bin/run_isql_prod_rpt_2.sh |
grep "^| Row_2" |
awk '{print $15 }'
which only works with echo in the front. I tried to set this line into a variable. I've tried quotations marks, parenthesis, and back ticks, with no luck.
May anyone tell me the correct syntax for setting this into a variable?
If you want more columns store in an array you should use this syntax (it is also good if you have only one result):
#!/bin/bash
result=( $( echo "exec [loc_ver].[dbo].[sp_RptEmpCheckInOutMissingTime_2]" |
/home/mdland_tool/common/bin/run_isql_prod_rpt_2.sh |
grep "^| Row_2" |
awk '{print $15 }' ) )
$result=$(exec [loc_ver].[dbo].[sp_RptEmpCheckInOutMissingTime_2]" | /home/mdland_tool/common/bin/run_isql_prod_rpt_2.sh | grep "^| Row_2" | awk '{print $15 })
Since you asked for both meanings of your question:
First, imagine a simpler case:
echo asdf
If you want to execute this command and store the result somewhere, you do the following:
$(echo asdf)
For example:
variable=$(echo asdf)
# or
if [ "$(echo asdf)" == "asdf" ]; then echo good; fi
So generally, $(command) executes command and returns the output.
If you want to store the text of the command itself, you can, well, just do that:
variable='echo asdf'
# or
variable="echo asdf"
# or
variable=echo\ asdf
Different formats depending on the content of your command. So now once you have the variable storing the command, you can simply execute the command with the $(command) syntax. However, pay attention that the command itself is in a variable. Therefore, you would execute the command by:
$($variable)
As a complete example, let's combine both:
command="echo asdf"
result=$($command)
echo $result

Creating bash aliases via .bashrc

As a sysadmin I routinely rdp and ssh into remote machines for administration.
I've created a file, ${SERVER_FILE}, containing one entry per line noting the hostname and protocol to use when connecting to a given server.
Example:
...
server1,ssh
winsrv,rdp
...
Given the above entries I want the following to be created+evaluated (rdp is itself a script on my system):
alias server1='ssh server1' winsrv='rdp winsrv'
The following code, when I cut and paste the resultant output to an alias command, works flawlessly:
$ echo $(sed "s/\(.*\),\(rdp\|ssh\)/\1='\2 \1' /g" ${SERVER_FILE} | tr -d '\n')
server1='ssh server1' winsrv='rdp winsrv'
$ alias server1='ssh server1' winsrv='rdp winsrv'
$ alias
alias server1='ssh server1'
alias winsrv='rdp winsrv'
SO I change it to this to actually cause the aliases to be created and I get errors:
$ alias $(sed "s/\(.*\),\(rdp\|ssh\)/\1='\2 \1' /g" ${SERVER_FILE} | tr -d '\n')
bash: alias: server1': not found
bash: alias: winsrv': not found
$ alias
alias server1=''\''ssh'
alias winsrv=''\''rdp'
Advice?
Try:
$ eval alias $(sed "s/\(.*\),\(rdp\|ssh\)/\1='\2 \1' /g" ${SERVER_FILE} | tr -d '\n')
Works for me.
Might I suggest awk instead of sed for a much more easily readable command?
awk 'BEGIN { FS=","
format = "%s=\"%s %s\" " }
$2 ~ /(rdp|ssh)/ { printf format, $1, $2, $1 }' ${SERVER_FILE}
Well, it looks like alias and echo are interpreting some backslashes differently. This is admittedly a hack, but I would try this:
alias $(echo $(sed "s/\(.*\),\(rdp\|ssh\)/\1='\2 \1' /g" ${SERVER_FILE} | tr -d '\n'))
:-)

Resources