Solaris - Comment Specific Line from File and Add New One - bash

I haven't worked much with solaris, but I'm supposed to be writing a script that searches for a line in a file, comments it out, and writes the correct line below it.
for i in `cat solarishosts`
do
#print hostname
echo ${i}
#get the line number of the expression after the /; save its value to linenum
linenum="$(ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "awk '/%sugrp ALL=\(user\) lines: /usr/bin/su -, /usr/bin/su - user/a{ print NR; exit }' /usr/local/etc/sudoers")"
#overwrite the line # linenum (overwriting just a to add a comment)
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "sed -n "${linenum}"p <<< "#%sugrp ALL=\(user\) lines: /usr/bin/su -, /usr/bin/su - user""
#use the linenum var to make a newlinenum var , this one being one line down from where the commented text was written
newlinenum=linenum+1
#write the line in quotes # the newlinenum position
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "sed -n "${newlinenum}"p <<< "%sugrp ALL=\(ALL\) ALL""
done
I'm getting weird errors :
awk: syntax error near line 1
awk: bailing out near line 1
bash: -c: line 0: syntax error near unexpected token `newline'
bash: -c: line 0: `sed -n p <<< #%sugrp ALL=(user) PASSWD: /usr/bin/su -, /usr/bin/su - user'
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `sed -n linenum+1p <<< %sugrp ALL=(ALL) ALL'
It looks like there's an error with my awk syntax ... but it isn't on line 1? And I'm not sure what the error is
I don't have a newline character anywhere in my first sed line?
In my code I escaped the "(' it's complaining about

That's pretty messy. You don't need to ssh into the box 3 times. Your quoting is a big problem. And you never actually write the changes back to the file.
Try this: build up the remote command and call ssh once:
line='%sugrp ALL=(user) lines: /usr/bin/su -, /usr/bin/su - user'
newline='%sugrp ALL=(ALL) ALL'
file=/usr/local/etc/sudoers
awkcmd='$0 == line {print "#" $0; print new}'
cmd=$(
printf "awk -v line='%s' -v new='%s' '%s' %s > %s.new && mv %s %s.bak && mv %s.new %s" \
"$line" \
"$newline" \
"$awkcmd" \
"$file" "$file" "$file" "$file" "$file" "$file"
)
while read -r host; do
echo "$host"
# perform the remote command
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 "$host" sh -c "$cmd"
done < solarishosts
I use single quotes as much as possible to reduce the need for backslashes in the constant strings, and all variables are quoted when used.

Related

Bash issue with unexpected token

The script below seems to work when I run it on its own in vscode it says literal carriage return on the second and third line.
When I put it into a larger script I get this issue
./script.sh: line 64: syntax error near unexpected token `newline'
./script.sh: line 64: `ipv6address=$(hostname -I | cut -d " " -f 2)'
Can any one advise please
#!/bin/bash
ipv6address=$(hostname -I | cut -d " " -f 2)
tee -a <<EOF >/dev/null "$HOME"/ipv6
[network]
# replace the ip with yours
routable_ip ="$ipv6address"
EOF

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

Concatenate String and Variable in Shell Script

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.

How to redirect grep to a while loop

Hi I have the following bash script code
group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")
and I get the following error:
./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'
I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside
The problem is in this line:
done << (grep -w "token" "$file")
# ^^
You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:
done < <(grep -w "token" "$file")
# ^ ^
Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

-bash: eval: line 109: unexpected EOF while looking for matching `"'

if [[ -s $GCMS_ENV && -r $GCMS_ENV ]]
then
echo "">/dev/null ##file exists
if [[ -s $GCMS_PRIV_ENV && -r $GCMS_PRIV_ENV ]]
then
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_PRIV_ENV | grep -v ^# 2>/dev/null 1> $TMP_FILE
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_ENV | grep -v ^# 2>/dev/null 1>> $TMP_FILE
else
egrep "[A-Z]?=.[a-zA-Z0-9]?" $GCMS_ENV | grep -v ^# 2>/dev/null 1> $TMP_FILE
fi
{
while read RECORD
do
VAR=$(echo $RECORD|cut -s -d$DELIM -f1)
VAL=$(echo $RECORD|cut -s -d$DELIM -f2-9)
eval export $VAR=$VAL
done
}<$TMP_FILE
else
echo "\n$THIS_FILE\n error:"
echo "The file $GCMS_ENV does not exist, no environment settings!"
return 1
fi
i am trying to run folllowing kSH shell in my linux box while running the same i am facing the following error.
-bash: eval: line 109: unexpected EOF while looking for matching `"'
-bash: eval: line 110: syntax error: unexpected end of file
please let me know anybody have a answer for the same
eval should be avoided whenever possible, especially when it is probably responsible for the syntax error you observe. Use this in place of your while loop.
while read -d"$DELIM" VAR VAL; do
declare -x "$VAR=$VAL"
done < "$TMP_FILE"
You may still have an error that eval triggered, so double check your input files to make sure the assignments have matched ".

Resources