bash setting variable getting error command not found [duplicate] - bash

This question already has answers here:
Indirect variable assignment in bash
(7 answers)
Closed 9 months ago.
I'm writing one small bash script for my Project
but i getting some errors
#!/bin/bash
count=$(cat Downloads/datei.csv | wc -l);
for((i=115;i<="122";i++)); do
line$i=$(sed -n $i"p" Downloads/datei.csv);
echo "line$i";
done
i trying to get every line from CSV in some variable
The erroĊ•
count.sh: line 4: line115=: command not found
if [[ -z "line$i" ]];
then
echo "$i is empty";
else
echo "$i is NOT empty";
fi
the second code gives me the same error

Suggest compose the code together, just update line$i to a temporary normal variable without other var like line_get.
One more thing, you need to update -z "line$i" to -z "$line_get". As in bash, you should use $ and the var name to get the value.
Looks like:
#!/bin/bash
count=$(cat Downloads/datei.csv | wc -l)
for((i=115;i<=122;i++)); do
line_get=$(sed -n $i"p" Downloads/datei.csv)
if [[ -z "$line_get" ]];
then
echo "$i is empty"
else
echo "$i is NOT empty"
fi
done
If you need to dynamic generate the variable name. Try this:
declare "line$i=$(sed -n $i"p" Downloads/datei.csv)"
var="line$i"
echo "${!var}"
And echo "line$i"; just print the text like line115 but not the value of the variable line$1.

Related

`echo` is stripping newlines in Bash script [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When should I double-quote a parameter expansion? [duplicate]
(1 answer)
Closed 5 months ago.
If I have a file containing newlines, the below script will output the file as is, with newlines:
#!/bin/bash
FOO=$(cat filename.yaml)
echo "$FOO"
but
#!/bin/bash
FOO=$(cat filename.yaml)
FOO=$(echo $FOO)
echo "$FOO"
outputs the file all on one line. How come?
I do not recommend storing the contents of entire files in a single variable. In my experience that can have unpredictable results.
/usr/bin/env bash -x
index=$(wc -l filename.yaml | cut -d' ' -f1)
count=1
next () {
[[ "${count}" -lt "${index}" ]] && main
[[ "${count}" -eq "${index}" ]] && exit 0
}
main () {
line=$(sed -n "${count}p" filename.yaml)
echo "var${count}=${line}" >> varfile
count=$(($count+1))
next
}
next
If you source varfile at the start of another script, it will give you every line from that file, in its' own variable.

Inserting to an shell array in a loop [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 2 years ago.
I am trying to insert values in to an array in a loop as follows:
branches_to_delete=()
cat branches.txt | while read branch_name
do
BRANCH_COUNT=$(git ls-remote | grep -w $branch_name | wc -l)
if [ $BRANCH_COUNT -eq 0 ]; then
echo "Branch does not exist"
branches_to_delete+=($branch_name)
elif [ $BRANCH_COUNT -eq 1 ]; then
echo "Branch exists"
else
echo "Not valid result"
fi
done
echo "Loop finished"
echo ${branches_to_delete[#]}
But when I printout branches_to_delete it is actually empty.
What am I doing wrong here?
With the pipe from cat branches.txt into the read loop, you create a subshell that cannot access the parent shell's branches_to_delete array.
You can fix this by avoiding the pipe and saving a useless use of cat:
branches_to_delete=()
while read branch_name; do
...
done < branches.txt
(Make sure nothing in ... reads from read's stdin. You'll notice if something is missing).

bash - setting variable value is not working [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 5 years ago.
Problem
My bash script loops through a bunch of files and if it finds an unexpected file that contains some text, it sets an error flag and is supposed to exit the loop. The problem is that when i evaluate the error condition after the loop, the error flag isn't set properly.
I can't seem to see where the bug is.
Code
#!/bin/sh
valid=1
grep -rs -L 'bogustext' * | while read line; do
echo "Processing file '$line'"
if [ "$line" != "arc/.keep" ] && [ "$line" != "arc/prp/lxc" ] && [ "$line" != "arc/lbb/lxc" ]; then
valid=0
echo "just set it to zero - $valid"
break 2
fi
done
echo "$valid"
if [ "$valid" -eq 0 ]; then
echo "1..1"
echo "not ok $line invis malformed"
else
echo "1..1"
echo "ok"
fi
Here's the output which shows the problem / bug. As you can see, there is an extra file in the list that contains the bogus string "arc/ttp". The script sets the "valid" variable to 0 but by the time I'm ready to evaluate it to display the right status, it's still the original value.
lab-1:/var/vrior# sh test.sh
Processing file 'arc/.keep'
Processing file 'arc/prp/lxc'
Processing file 'arc/lbb/lxc'
Processing file 'arc/ttp'
just set it to zero - 0
1
1..1
ok
What I've tried so far
I'm currently googling to see if in Bash there's local variables vs. global. Not too sure, but if you see my bug, please let me know.
Pipes create subshells, so $valid inside the loop refers to a new variable, which disappears before you try and use it.
Since you've tagged bash, then you can use a process substitution instead of the pipe:
while read line; do
echo "Processing file '$line'"
if [ "$line" != "arc/.keep" ] && [ "$line" != "arc/prp/lxc" ] && [ "$line" != "arc/lbb/lxc" ]; then
valid=0
echo "just set it to zero - $valid"
break 2
fi
done < <(grep -rs -L 'bogustext' *)

setting variables with variable names in bash (para$i=... giving "command not found") [duplicate]

This question already has answers here:
Assign variables inside for loops
(2 answers)
Closed 6 years ago.
I want to pass three arguments to a script,the first two numbers and third one any character,Buut when i run the script it says command not found ,even though the value is getting assigned.i have attached the code and image below.enter image description here
This is my peice of code,
#!/bin/bash
if [ $# -lt 3 ]
then
echo "insufficient argument"
for((i=$#+1;i<4;i=$i+1))
do
read -p "enter $i parameter: " x
para$i=x
done
fi
This is not a valid assignment:
para$i=x
Since your shell is bash, you can do the following instead:
# bash 3.1 or higher
printf -v "para$i" %s "$x"
...or...
# bash 4.3 or higher; works with arrays and other tricky cases too.
declare -n para="para$i"
para=$x
unset -n para
...or...
# any POSIX shell
# be very careful about the quoting; only safe if $x is quoted and $i is a controlled value
eval "para$i=\$x"
See the BashFAQ #6 section on indirect assignment for more details.
See an example of processing a parameter called process_date
The script this will accept the parameter as so:
some_sh_script.sh -process_date=01/01/2016
Script:
process_date=""
while test "$1" != "" ; do
# Test argument syntax e.g. -someName=someValue or help operators
if [[ $1 != -*=* && $1 != -h && $1 != -help ]]
then
echo "Error in $0 - $1 - Argument syntax invalid."
usage
exit 1
fi
# END Test argument syntax
# Split argument name & value by `=` delimiter
paramName=`echo $1 | cut -d '=' -f1`
paramVal=`echo $1 | cut -d '=' -f2`
case $paramName in
-process_date)
process_date=$paramVal
;;
#User help parameter
-help|-h)
usage
exit 0
;;
-*)
echo "No such option $1"
usage
exit 1
;;
esac
#parse next argument
shift
done

Matching a list of ip addresses with another file with ip addresses [duplicate]

This question already has answers here:
How to find a list of ip addresses in another file
(3 answers)
Closed 8 years ago.
I was given the task to retrieve a long list of ip addresses from another file with ip addresses. I created this bash script but it does not work very well. After executing the script I check the file called "found" there is nothing, and when I check the file called "notfound" there are about 60 ip addresses. In both files there has to be a total of 1500 ip addresses. There are two files; 1. list of ip addresses to retrieve(findtheseips.txt), 2. list of ip addresses to retrieve from(listips.txt). Can anybody please help me to make it work. Thank you very much. I run the script this way: ./script findtheseips.txt
#!/bin/bash
declare -a ARRAY
exec 10<&0
exec < $1
let count=0
while read LINE; do
ARRAY[$count]=$LINE
if egrep "$LINE" listips.txt; then
echo "$LINE" >> found
else
echo "$LINE" >> notfound
fi
done
There's no need to try to use exec or to create an array.
You could read from the script's first argument $1.
There shouldn't be a need to use egrep unless you're trying to do extended regular expression matching.
#!/bin/bash
while read LINE; do
if grep "$LINE" listips.txt; then
echo "$LINE" >> found
else
echo "$LINE" >> notfound
fi
done < $1
Here is an all BASH solution.
#!/bin/bash
while read l1; do
n=0
while read l2; do
if [[ $l1 == $l2 ]]; then
echo "$l1" >> found
((n++))
fi
done < ips2
if [ $n -eq 0 ]; then
echo "$l1" >> notfound
fi
done < $1

Resources