How to use cat in a pipe - bash

I have the following command:
httpd.conf | grep AuthUserFile | cut -d" " -f4 | sed -e 's|["'\'']||g'
the output of this is:
/etc/httpd/secure/htpasswd.training
I did:
httpd.conf | grep AuthUserFile | cut -d" " -f4 | sed -e 's|["'\'']||g'| cat
However this just returned:
/etc/httpd/secure/htpasswd.training
I want to cat the contents of the file. How do I do this?

Piping to xargs cat will pass stdin as an argument to cat, printing the file.
Alternatively try: cat $( some command printing a filename ).

You could try surrounding your initial command with backticks as the argument to cat, like so:
cat `httpd.conf | grep AuthUserFile | cut -d" " -f4 | sed -e 's|["'\'']||g'`

Related

How to pass environment variable to a command passed to xmllint?

I'm trying to extract the db element's value attributes from an xml file using xmllint
My XPath Query needs to navigate to the correct dbtype, which I wish to store in an environment variable
Without an environment variable (hard coded), this command works
echo 'cat //rdbmsinfo/dbtype[#value="sqlserver"]/db/#value' | xmllint --shell "config.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \ | sed 's/"//g'
I am trying with below command:
echo 'cat //rdbmsinfo/dbtype[#value="$ldb_source_typ"]/db/#value' | xmllint --shell "config.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \ | sed 's/"//g'
where ldb_source_typ is a variable to get the value from shell parameter as "sqlserver". But the above syntax is not giving any output.
Why isn't this working, and how can I fix it?
My config.xml XML looks like this:
<?xml version="1.0"?>
<rdbmsinfo>
<dbtype value="sqlserver">
<db value="sqlsrv1">
<dbhostip>192.168.0.1</dbhostip>
<dbhostportno>2000</dbhostportno>
<dbusername>sample</dbusername>
<dbpassword>sample</dbpassword>
</db>
<db value="sqlsrv2">
<dbhostip>192.168.0.2</dbhostip>
<dbhostportno>2000</dbhostportno>
<dbusername>sample</dbusername>
<dbpassword>sample</dbpassword>
</db>
</dbtype>
<dbtype value="postgresql">
<db value="postsql1">
</db>
</dbtype>
</rdbmsinfo>
As I just found out, you need to set the environment variable between single quotes ( ' ).
So the following should work:
echo 'cat //rdbmsinfo/dbtype[#value="'$ldb_source_typ'"]/db/#value' | xmllint --shell "config.xml" | grep -v ">" | cut -f 2 -d "=" | tr -d \ | sed 's/"//g'
I was able to accomplish this with the following:
echo "cat //rdbmsinfo/dbtype[#value="ldb_source_type"]/db/#value" | sed 's/ldb_source_type/"'$SQLSERVER'"/g' | xmllint --shell config.xml| grep -v ">" | cut -f 2 -d "=" | tr -d \ | sed 's/"//g'
Which produces the following output:
sqlsrv1
-------
sqlsrv2
I didn't care enough to figure out how to make the value placeholder use an $ symbol, I may come back and edit it eventually.
Edit: here's one with a $, but you need to escape it with a :
echo "cat //rdbmsinfo/dbtype[#value="\$ldb_source_type"]/db/#value" | sed 's/\$ldb_source_type/"'$SQLSERVER'"/g'| xmllint --shell config.xml| grep -v ">" | cut -f 2 -d "=" | tr -d \ | sed 's/"//g'

Bash script any reason why it wont write the file

I am helping debug some code that exec the following script is there any reason why its not writing a file to the server? - if that what it does. All the $ data and permissions are ok:
Script:
#!/bin/bash
RANGE=$1
ALLOCATION=`echo $RANGE | cut -f1,2,3 -d'.'`
/sbin/ip rule add from $1 lookup $2
echo $ALLOCATION
rm /path/too/file/location/$ALLOCATION
for i in `seq 3 254`
do
echo $ALLOCATION.$i >> /path/too/file/location/$ALLOCATION
done
ETH=`/sbin/ifconfig | grep eth0 | tail -n1 | cut -f2 -d':' | cut -f1 -d' '`

Grep Spellchecker

I am trying to write a simple shell script that takes a text file as input and checks all non-punctuated words against a dictionary (english.txt). It should return all non-matching (misspelled) words. I am using grep but it does not seem to successfully match all the lines in english.txt. I have included my code below.
#!/bin/bash
cat $1 |
tr ' \t' '\n\n' |
sed -e "/'/d" |
tr -d '[:punct:]' |
tr -cd '[:alpha:]\n' |
sed -e "/^$/d" |
grep -v -i -w -f english.txt

Bash script builds correct $cmd but fails to execute complex stream

This short script scrapes some log files daily to create a simple extract. It works from the command line and when I echo the $cmd and copy/paste, it also works. But it will breaks when I try to execute from the script itself.
I know this is a nightmare of patterns that I could probably improve, but am I missing something simple to just execute this correctly?
#!/bin/bash
priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"
cmd="grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort >$outputfile"
printf "command to execute:\n"
echo $cmd
printf "\n"
$cmd
ouput:
./make_log_extract.sh command to execute: grep 'Processed inbound' /home/rules/care/logs/RootLog.log /home/rules/care/logs/RootLog.log.1
/home/rules/care/logs/RootLog.log.10
/home/rules/care/logs/RootLog.log.11
/home/rules/care/logs/RootLog.log.12
/home/rules/care/logs/RootLog.log.2
/home/rules/care/logs/RootLog.log.3
/home/rules/care/logs/RootLog.log.4
/home/rules/care/logs/RootLog.log.5
/home/rules/care/logs/RootLog.log.6
/home/rules/care/logs/RootLog.log.7
/home/rules/care/logs/RootLog.log.8
/home/rules/care/logs/RootLog.log.9 | cut -f5,6,12,16,18 -d" " | grep
'^2014-01-30' | sed 's/\,/./' | sed 's/ /\t/g' | sed -r
's/([0-9]+-[0-9]+-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort
/home/CCHCS/da14/2014-01-30_PROD_message_processing_times.txt
grep: 5,6,12,16,18: No such file or directory
As grebneke comments, do not store the command and then execute it.
What you can do is to execute it but firstly print it: Bash: Print each command before executing?
priorday=$(date --date yesterday +"%Y-%m-%d")
outputfile="/home/CCHCS/da14/$priorday""_PROD_message_processing_times.txt"
set -o xtrace # <-- set printing mode "on"
grep 'Processed inbound' /home/rules/care/logs/RootLog* | cut -f5,6,12,16,18 -d\" \" | grep '^"$priorday"' | sed 's/\,/\./' | sed 's/ /\t/g' | sed -r 's/([0-9]+\-[0-9]+\-[0-9]+)\t/\1 /' | sed 's/ / /g' | sort >$outputfile"
set +o xtrace # <-- revert to normal

Use each line of piped output as parameter for script

I have an application (myapp) that gives me a multiline output
result:
abc|myparam1|def
ghi|myparam2|jkl
mno|myparam3|pqr
stu|myparam4|vwx
With grep and sed I can get my parameters as below
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//'
But then want these myparamx values as paramaters of a script to be executed for each parameter.
myscript.sh myparam1
myscript.sh myparam2
etc.
Any help greatly appreciated
Please see xargs. For example:
myapp | grep '|' | sed -e 's/^[^|]*//' | sed -e 's/|.*//' | xargs -n 1 myscript.sh
May be this can help -
myapp | awk -F"|" '{ print $2 }' | while read -r line; do /path/to/script/ "$line"; done
I like the xargs -n 1 solution from Dark Falcon, and while read is a classical tool for such kind of things, but just for completeness:
myapp | awk -F'|' '{print "myscript.sh", $2}' | bash
As a side note, speaking about extraction of 2nd field, you could use cut:
myapp | cut -d'|' -f 1 # -f 1 => second field, starting from 0

Resources