Concatenate String and Variable in Shell Script - bash

Content of file is:
#data.conf
ip=127.0.0.1
port=7890
delay=10
key=1.2.3.4
debug=true
Shell Script:
#!/bin/bash
typeset -A config
config=()
config_file_path="./data.conf"
cmd="java -jar ./myprogram.jar"
#This section will read file and put content in config variable
while read line
do
#echo "$line"
if echo $line | grep -F = &>/dev/null
then
key=$(echo "$line" | cut -d '=' -f 1)
config[$key]=$(echo "$line" | cut -d '=' -f 2)
echo "$key" "${config["$key"]}"
fi
done < "$config_file_path"
cmd="$cmd -lh ${config["ip"]} -lp ${config["port"]} -u ${config["debug"]} -hah \"${config["key"]}\" -hap ${config["delay"]}"
echo $cmd
Expected output:
java -jar myprogram.jar -lh 127.0.0.1 -lp 7890 -u true -hah "1.2.3.4" -hap 10 -b
Output:
Every time some unexpected o/p
Ex. -lp 7890rogram.jar
Looks like it is overwriting same line again and again

In respect to the comments given and to have an additional automatic data cleansing within the script, you could have according How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script? and Remove carriage return in Unix
# This section will clean the input config file
sed -i 's/\r$//' "${config_file_path}"
within your script. This will prevent the error in future runs.

Related

Git Bash script echo command output inverted after parameter

i'm using git bash for windows (git version 2.18.0.windows.1) to write a bash script like this one:
file=$1
if [[ ! -s "$file" ]] ; then #empty
echo -e "${RED}Invalid argument. Pass the file with all GCIDs as INPUT!!!${NOCOLOR}"
else
number=$(cat $file | wc -l )
number=$(($number+1))
echo -e "** ${number} GCID detected **"
echo ""
while read -r gcidRead
do
gcid=${gcidRead}
echo -e "select distinct operation from audit_trail.audit_trail where gcid='$gcid';" >> query.txt
value=$(psql "host=XXXX port=62013 dbname=prodemeagcdm user=XXXX password=XXXX" <<-EOF
select distinct operation from audit_trail.audit_trail where gcid='$gcid';
\q
EOF
)
echo -e "${value}" >> output.txt
if grep -q delete_bupa output.txt ; then
echo -e "${gcid}" >> gcidDeleted.txt
fi
done < $file
fi
I created just to debug the query.txt file in which the output is:
';lect distinct operation from audit_trail.audit_trail where gcid='XXX
instead of
select distinct operation from audit_trail.audit_trail where gcid='XXX'
In short, every string after $gcid parameter will be written at the beginning of the entire string.
If I use a unix terminal the echo output is ok.
Why in git bash the "echo" command has the wrong output mentioned?
Thanks in advance
I think you see the output on terminal of a string which contains a CR (13 in dec or 0D in hex) : the last '; cahracters are written from the beginning of the line, thus overwriting the first two characters of the string.
The string actually consists of select dist... gcid='XXX\r';, and is just printed awkwardly (to a human) on the terminal.
There are many ways to drop CR from the input, here are two of them :
# remove all CR chars from the input :
cat $file | tr -d '\r' | while read -r gcdiRead; do
...
done
# remove all CR chars only at end of lines (e.g : when followed by LF) :
cat $file | sed -e 's/\r$//' | while read -r gcdiRead; do
...
done

grep: No such file or directory for line in file

I'm trying to grep every line within a file, and for every match echo PASS, for every non match echo FAIL. I am getting a "no such file or directory" error, it seems to be trying to grep a file instead of the line I am passing within my loop?
File:
$ cat new.txt
TLS_RSA_WITH_AES_128_GCM_SHA256
TLS_RSA_WITH_AES_128_CBC_SHA256
TLS_RSA_WITH_AES_128_CBC_SHA
TLS_RSA_WITH_3DES_EDE_CBC_SHA
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
Failing script:
#!/bin/bash
while IFS= read -r line
do
if grep -P 'TLS_RSA' $line; then
echo "PASS."
else
echo "FAIL."
fi
done < "new.txt"
When running a normal grep command against the file this is the output (expected):
$ grep -P 'TLS_RSA' new.txt
TLS_RSA_WITH_AES_128_GCM_SHA256
TLS_RSA_WITH_AES_128_CBC_SHA256
TLS_RSA_WITH_AES_128_CBC_SHA
TLS_RSA_WITH_3DES_EDE_CBC_SHA
I suggest to replace
if grep -P 'TLS_RSA' $line; then
with
if grep -q 'TLS_RSA' <<< "$line"; then
to feed grep's stdin with content of a variable.
#!/bin/bash
while IFS= read -r line; do
grep -q 'TLS_RSA' <<< $line && echo "PASS."|| echo "FAIL."
done < "new.txt"
You need to replace the $line with <<< $line.
Also for the sake of cleaner code, you can also do away with the if statement and make it into a single line.
You can also use awk as an alternative to looping in a script
awk '{ print "grep -o \"TLS_RSA\" "$0" && echo -e \""$0"\nPASS\" || echo -e \""$0"\nFAIL\"" }' new.txt | bash
This prints the grep command for each line (file) in the new.txt file and then pipes the command through to bash to execute
As an alternative, you can use awk's built in system funtion and so:
awk '{ system("grep -o \"TLS_RSA\" "$0" && echo -e \""$0"\nPASS\" || echo -e \""$0"\nFAIL\"") }' new.txt
NOTE - You need to be careful with command indirection examples such as these. It is important that you print and verify the command (i.e. run the command with piping through to bash in the first example) before executing the actual commands.
You can do the same without grep but just built-in POSIX shell features:
#!/usr/bin/env sh
while IFS= read -r line
do
case $line in
*TLS_RSA*) printf %s\\n PASS.;;
*) printf %s\\n FAIL.;;
esac
done <new.txt

How to run commands off of a pipe

I would like to run commands such as "history" or "!23" off of a pipe.
How might I achieve this?
Why does the following command not work?
echo "history" | xargs eval $1
To answer (2) first:
history and eval are both bash builtins. So xargs cannot run either of them.
xargs does not use $1 arguments. man xargs for the correct syntax.
For (1), it doesn't really make much sense to do what you are attempting because shell history is not likely to be synchronised between invocations, but you could try something like:
{ echo 'history'; echo '!23'; } | bash -i
or:
{ echo 'history'; echo '!23'; } | while read -r cmd; do eval "$cmd"; done
Note that pipelines run inside subshells. Environment changes are not retained:
x=1; echo "x=2" | while read -r cmd; do eval "$cmd"; done; echo "$x"
You can try like this
First redirect the history commands to a file (cut out the line numbers)
history | cut -c 8- > cmd.txt
Now Create this script hcmd.sh(Referred to this Read a file line by line assigning the value to a variable)
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
$line
done < "cmd.txt"
Run it like this
./hcmd.sh

extract file content using a bash script

It has been long time since my last bash script.
I m just trying to extract the content of a file from the the start variable to the stop one.
My source file is night4.info and it contains a list of .jpg files.The structure of this file is similar to:
./2014-11-02/18h/00mn/2014-11-02T18-00-00.048000-depth.jpg
./2014-11-02/18h/00mn/2014-11-02T18-00-00.182000-depth.jpg
./2014-11-02/18h/00mn/2014-11-02T18-00-00.316000-depth.jpg
This is the code so far :
#! /bin/bash
start=$(grep -n $1 night4.info | cut -d : -f 1)
stop=$(grep -n $2 night4.info | cut -d : -f 1)
echo "1" >> list.info
sed -n -e "$start,$stop p" night4.info >> list.info
And this is how I m running my script:
./script1.sh 2014-11-02T18-00-00.048000 2014-11-03T06-59-59.981000
There is no error message and the code doesn't give the right output.
You could use a Perl one-liner with the range operator:
perl -ne "print if /\Q$1\E/../\Q$2\E/" night4.info >> list.info

Execute commands from shell script fails

I'm trying read a file which contains lines like this:
Run COMMAND with options "OPTIONS" and arguments "ARGUMENTS"
Then I want to execute this command with given options and arguments. For example I'd like to execute these commands:
Run pwd with options "" and arguments ""
Run ls with options "-al" and arguments "$HOME"
Run ls with options "-al" and arguments "Example: \"strange folder name\""
This is my code
#!/bin/bash
while read -r line
do
COMMAND=$(echo "$line" | cut -d" " -f 2)
OPTIONS=$(echo "$line" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(echo "$line" | cut -d" " -f 8)
$COMMAND $OPTIONS $ARGUMENTS
done <$1
First example is working as it should, second one is giving me error ls: cannot access $HOME: No such file or directory' and third one is not storing the name of the folder to $ARGUMENTS correctly.
second one is giving me error ls: cannot access $HOME: No such file or directory'
This is because the folder named $HOME does not exist. I am not talking about the value of $HOME variable, but the string literal. The shell does not execute the parameter expansion in your situation.
third one is not storing the name of the folder to $ARGUMENTS correctly
This is because -f 8 only extract column 8, try -f 8- to extract the 8th column and all the others until the end of line.
You can give a try to this version below:
while read -r line; do
COMMAND=$(printf "%s" "${line}" | cut -d" " -f 2)
OPTIONS=$(printf "%s" "${line}" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(printf "%s" "${line}" | cut -d" " -f 8-)
$COMMAND $OPTIONS "$(eval printf \"%s\" "$ARGUMENTS")"
done < "${1}"
The eval is a shell built-in command which is used to enable parameter expansion of ARGUMENTS, if applicable.
I have to warn you that the eval is usualy say risky to use.

Resources