cannot modify variables inside bash script case statement using while [duplicate] - linux

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 3 years ago.
I have s simple bash script as follow:
interval=""
cat conf.param|\
while read param
do
item_=$(echo $param|cut -d "=" -f1)
case ${item_} in
interval)
interval=$(echo $param|cut -d "=" -f2)
echo $interval
;;
method)
method=$(echo $param|cut -d "=" -f2)
;;
esac
done
echo "${interval}"
It reads the contents of a file and stores them in different variables. the issue is that the variables are not set properly inside case segment. I put two echos. The first one (inside case) displays the interval value correctly which is '2', but the second one just after the esac statement displays nothing! it shows an empty blank line.
the conf.param is a simple text file. it has more lines I only printed two lines:
interval=2
method="POST"

Your problem is that using a pipe ("cat conf.param | while read param"), you call a second shell which can not export its variables to the calling one. See this example:
interval=""
cat tmp.txt | while read param; do ##### DON'T DO THIS
interval="A$interval"
done
echo "First attempt: $interval"
interval=""
while read param; do
interval="A$interval"
done < tmp.txt ##### BUT DO THIS INSTEAD
echo "Redirection attempt: $interval"
The file tmp.txt contains 4 lines; the output of the script is:
First attempt:
Redirection attempt: AAAA
As you see, the first attempt retains the old value of interval. The second/redirection attempt instead works because no new process is created.

Related

How to echo this entire code to .bashrc without leaving out characters/strings? [duplicate]

This question already has answers here:
How do I properly quote this bash pipeline for watch?
(2 answers)
Closed 3 years ago.
How can I echo this entire piece of code to .bashrc without leaving out a single character?
# automatic logging of terminal input/output
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/$(date +"%Y-%m- %d_%H:%M:%S")_terminal.log)
When I attempt to enter the following into terminal:
echo "the above code" >> ~/.bashrc
I get the following appended to .bashrc which is nothing like "the above code", its short about 45 or so characters.
# automatic logging of terminal input/output
test script == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/2019-05- 08_09:09:19_terminal.log)
As you can see, it's leaving out A LOT of the original code. I understand this has a lot to do with the number of different quotations and placement, but without altering my code much, or at least to the point where it can still function as its intended, how can I go about getting this to echo to the file properly?
Thank you for every nanosecond of your time.
Wrap your echo'd string with single quotes ' instead of double "

Read first characters of a variable in bash script? [duplicate]

This question already has answers here:
In Bash, how can I check if a string begins with some value?
(13 answers)
Closed 6 years ago.
I have a file where i get some informations through a bash script to put data in a DB table and i'd like to know how to read the first characters of a variable because if it starts with "CE-" that line's data will go into a table if not they must be inserted in an other one, how can i do this?
Like this-
var=CE-xxxxx
echo "$var"
output- CE-xxxxx
var2=$(echo "$var" | cut -c 1-3)
echo "$var2"
output- CE-
Then you can check if $var2 matches your criteria and use it further.
You can use cut to get the bytes that you need:
V="CE-IMPORTANT"
I=$(echo $V | cut -b 4-)
If you want to use the - as separator:
I=$(echo $V | cut -d '-' -f 2)
In both cases you get "IMPORTANT" in I var

Bash shell script update and print a variable overwriting the same line [duplicate]

This question already has answers here:
Displaying only single most recent line of a command's output
(2 answers)
Closed 5 years ago.
I been trying to print a variable in the same time for a scrip that pretends automatize a process the content is the output of this
sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP
so i put this in a while loop
do
out= sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP
echo -n "$out"
sleep 1
done
i read other questions here and i try with different option like echo -ne, echo -ne "$out" \r, printf "\r" or printf "%s" and no luck with no one, all the other example don't have a variable to print just counter o system variables
Update
it seems to appear that the echo -n repeat $out in the same line, if out="this is a test" the output of echo -n is "this is a test this is a test this is a test this is a test ...." maybe im missing some option ?
Update 2
sorry for the miss understood perhaps i was not very clear but what i want is overwrite the same line with the value of $out, the source of $out is the output of the aireplay-ng command that executes along with the script and i get the output with
the ouput is something like this
102415 packets (got 5 ARP requests and 15438 ACKs), sent 37085 packets...(499 pps)
but the number of ARP request is changing constantly
this code for example use echo -ne and overwrite in the same line
#!/bin/bash
for pc in $(seq 1 100); do
echo -ne "$pc%\033[0K\r"
sleep 1
done
the output of this is like a percent indicator that shows "10%" and going instead of "1% 2% 3% 4% 5% .." in the same line and i already try like this but with no luck
if you are trying to execute the sed Please use
`sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP`
First of all you are assigning a value of bash command wrongly to variable.
out=$(sed "s/Read/\n/g" /tmp/Air/test.txt | tail -1 test.txt | grep ARP)
Then you can print all your output in one line as you wrote:-
echo -n $out
The recent addendum to your question reads like you're miscommunicating your intent: this is a test this is a test this is a test is what a plain reading of your question indicates you to be asking for (printing this is a test over and over in a loop without newlines, after all, can be expected to do nothing else); why you'd describe this in a context that makes it sound like a bug is thus surprising.
If you want to send the cursor back to the beginning of your current line and overwrite that line, that might be something like the following:
#!/bin/bash
# ^^^^ not /bin/sh; this enables bash extensions
# ask the shell to keep $COLUMNS up-to-date
shopt -s checkwinsize
# defaults to 80-character terminal width, but uses $COLUMNS if available
printf "%-${COLUMNS:-80}s\r" "$out"`
...which prints your string, pads out to 80 characters with spaces, and then returns the cursor to the beginning of the line, such that the next thing you write will overwrite that string.
Of course, if you print that line and then return to a shell prompt, the prompt will start at the beginning of the same line and overwrite the text, so be sure to follow up with an echo.

Looping in bash [duplicate]

This question already has answers here:
while loop to read file ends prematurely
(4 answers)
Closed 7 years ago.
I have a do-while loop in bash, and it runs the body once but no more.
while read id; do
bash ./setup_instance.sh "${id}"
echo "Ran setup_instance"
done < ids.txt
echo "Finished of loop"
The file ids.txt has two lines, one "ID" string per line. I tested this by replacing the line inside the loop with echo "ID: ${id}" and the loop works.
Also, note that the code does get to the two echo lines, so it is not the case that invoking the script throws us out of the execution.
What am I doing wrong?
When running
while read id; do
echo "Ran setup_instance ${id}"
done < ids.txt
echo "Finished of loop"
with ids.txt:
abc
bcd
I get the expected output (with the two lines).
They is a chance that your last line does not contain a linefeed (\n)
Can you check by running cat -A ids.txt
It should echo
abc$
bcd$
If the second dollar is missing, try running dos2unix ids.txt to add the missing newline (or add it manually)

Bash. Shell variables logic [duplicate]

This question already has an answer here:
Variables set in a bash 'while read' loop are unset after it [duplicate]
(1 answer)
Closed 8 years ago.
I have the following script:
ls -l | while read permissions number user user2 size month day hour filename
do
if [[ "$filename" == *foo* ]]
then
scount=`expr $scount + $size`
fi
done
echo $scount
The script checks the filename and size of the files in my folder, then it checks for files which contain 'foo' word, then it takes it size and adds it.
I have the following problem, if I call the scount variable from inside the loop it displays the numbers, but when it is outside it shows nothing, as if the variable does not exist. Does it have something to do with shell sessions? How can I display the variable outside of the loop?
Your while loop was run in a subshell so its variables are not visible outside. Try this one:
#!/bin/bash
while read current; do
echo "current: $current"
last=$current
done < <(echo -e "foo\nbar\nbaz")
echo "last: $last"

Resources