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' *)
Related
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.
This question already has answers here:
Loop over array, preventing wildcard expansion (*)
(2 answers)
Closed 11 months ago.
I need to change some commented key value pairs in the file /etc/postfix/main.cf. Rather than changing the values i'm trying to append those key value pair at the bottom of the file, because this also works. So I've kept those key value pairs in a separate file and trying to fetch those pairs as a string in my code. But while fetching those pairs from the file, only the first string in code is getting fetched in the loop and not the later ones. I don't understand where i'm going wrong in the code. Here is my code -
file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=$'\n'
var1=($(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all$' $file1))
echo ${var1[0]}
echo ${var1[1]}
echo ${var1[2]}
for line in $var1
do
grep -q $line $file2
if [ $? -eq 1 ]
then
echo "Strings are added to the target file!"
echo $line >> $file2
else
echo "String already exist in the file!"
fi
done
file content of the file 'conf.txt' -
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
output I get at the end of main.cf-
mynetworks = 127.0.0.0/8, 168.100.189.0/28
Output I desire at the end of the main.cf-
mynetworks = 127.0.0.0/8, 168.100.189.0/28
myhostname = centos7.example.com
inet_interface = all
I think your problem in in var1 variable. Try something like this:
file1=/root/conf.txt
file2=/etc/postfix/main.cf
IFS=$'\n'
for line in $(grep -E '^mynetworks|^myhostname.*com$|^inet_interface.*all$' $file1 )
do
grep -q $line $file2
if [ $? -eq 1 ]
then
echo "Strings are added to the target file!"
echo $line >> $file2
else
echo "String already exist in the file!"
fi
done
I have a following list.txt file with the content
cat list.txt
one
two
zero
three
four
I have a shell script (check.sh) like below,
for i in $(cat list.txt)
do
if [ $i != zero ]; then
echo "the number is $i"
else
exit 1
fi
done
it gives output like below,
./check.sh
the number is one
the number is two
I want to have script which continue with the rest of the items in the list.txt, but it should not process zero and continue with the rest of item.
eg.
the number is one
the number is two
the number is three
the number is four
I tried using "return" but it did not work, gave error.
./check.sh: line 6: return: can only `return' from a function or sourced script
About exit (and return)
The command exit will quit running script. There is no way to continue.
As well, return command will quit function. There in no more way to continue.
About reading input file
For processing line based input file, you'd better to use while read instead of for i in $(cat...:
Simply try:
while read -r i;do
if [ "$i" != "zero" ] ;then
echo number $i
fi
done <list.txt
Alternatively, you could drop unwanted entries before loop:
while read -r i;do
echo number $i
done < <( grep -v ^zero$ <list.txt)
Note: In this specific case, ^zero$ don't need to be quoted. Consider quoting if your string do contain special characters or spaces.
If you have more than one entries to drop, you could use
while read -r i;do echo number $i ;done < <(grep -v '^\(zero\|null\)$' <list.txt)
Alternatively, once input file filtered, use xargs:
If your process is only one single command, you could avoid bash loop by using xargs:
xargs -n 1 echo number < <(grep -v '^\(zero\|null\)$' <list.txt)
How to use continue in bash script
Maybe you are thinking about something like:
while read -r i;do
if [ "$i" = "zero" ] ;then
continue
fi
echo number $i
done <list.txt
Argument of continue is a number representing number of loop to shortcut.
Try this:
for i in {1..5};do
for l in {a..d};do
if [ "$i" -eq 3 ] && [ "$l" = "b" ] ;then
continue 2
fi
echo $i.$l
done
done
(This print 3.a and stop 3 serie at 3.b, breaking 2 loop level)
Then compare with
for i in {1..5};do
for l in {a..d};do
if [ "$i" -eq 3 ] && [ "$l" = "b" ] ;then
continue 1
fi
echo $i.$l
done
done
(This print 3.a , 3.c and 3.d. Only 3.b are skipped, breaking only 1 loop level)
I was doing this little script in which the first argument must be a path to an existing directory and the second any other thing.
Each object in the path indicated in the first argument must be renamed so that the new
name is the original that was added as a prefix to the character string passed as the second argument. Example, for the string "hello", the object OBJECT1 is renamed hello.OBJECT1 and so on
Additionally, if an object with the new name is already present, a message is shown by a standard error output and the operation is not carried out continuing with the next object.
I have the following done:
#! /bin/bash
if [ "$#" != 2 ]; then
exit 1
else
echo "$2"
if [ -d "$1" ]; then
echo "directory"
for i in $(ls "$1")
do
for j in $(ls "$1")
do
echo "$i"
if [ "$j" = "$2"."$i" ]; then
exit 1
else
mv -v "$i" "$2"."$i"
echo "$2"."$i"
fi
done
done
else
echo "no"
fi
fi
I am having problems if I run the script from another file other than the one I want to do it, for example if I am in /home/pp and I want the changes to be made in /home/pp/rr, since that is the only way It does in the current.
I tried to change the ls to catch the whole route with
ls -R | sed "s;^;pwd;" but the route catches me badly.
Using find you can't because it puts me in front of the path and doesn't leave the file
Then another question, to verify that that object that is going to create new is not inside, when doing it with two for I get bash errors for all files and not just for coincidences
I'm starting with this scripting, so it has to be a very simple solution thing
An obvious answer to your question would be to put a cd "$2 in the script to make it work. However, there are some opportunities in this script for improvement.
#! /bin/bash
if [ "$#" != 2 ]; then
You might put an error message here, for example, echo "Usage: $0 dir prefix" or even a more elaborate help text.
exit 1
else
echo $2
Please quote, as in echo "$2".
if [ -d $1 ]; then
Here, the quotes are important. Suppose that your directory name has a space in it; then this if would fail with bash: [: a: binary operator expected. So, put quotes around the $1: if [ -d "$1" ]; then
echo "directory"
This is where you could insert the cd "$1".
for i in $(ls $1)
do
It is almost always a bad idea to parse the output of ls. Once again, this for-loop will fail if a file name has a space in it. A possible improvement would be for i in "$1"/* ; do.
for j in $(ls $1)
do
echo $i
if [ $j = $2.$i ]; then
exit 1
else
The logic of this section seems to be: if a file with the prefix exists, then exit instead of overwriting. It is always a good idea to tell why the script fails; an echo before the exit 1 will be helpful.
The question is why you use the second loop? a simple if [ -f "$2.$i ] ; then would do the same, but without the second loop. And it will therefore be faster.
mv -v $i $2.$i
echo $2.$i
Once again: use quotes!
fi
done
done
else
echo "no"
fi
fi
So, with all the remarks, you should be able to improve your script. As tripleee said in his comment, running shellcheck would have provided you with most of the comment above. But he also mentioned basename, which would be useful here.
With all that, this is how I would do it. Some changes you will probably only appreciate in a few months time when you need some changes to the script and try to remember what the logic was that you had in the past.
#!/bin/bash
if [ "$#" != 2 ]; then
echo "Usage: $0 directory prefix" >&2
echo "Put a prefix to all the files in a directory." >&2
exit 1
else
directory="$1"
prefix="$2"
if [ -d "$directory" ]; then
for f in "$directory"/* ; do
base=$(basename "$f")
if [ -f "Sdirectory/$prefix.$base" ] ; then
echo "This would overwrite $prefix.$base; exiting" >&2
exit 1
else
mv -v "$directory/$base" "$directory/$prefix.$base"
fi
done
else
echo "$directory is not a directory" >&2
fi
fi
Ok, so now I have it setup like this but still it is giving me a ; exit;
logout
[Process completed]
when run through the terminal I know that there are configuration profiles that are not on my computer that should make the script continue or rather to keep looping. What is this not happening?
Thanks in advance....!
Here is what i have so far:
#!/bin/bash
profilesInstalled=`profiles -P|awk '/attribute/ {print $4}'`
while read line ; do
if [ "$line" = "F2CC78D2-A63F-45CB-AE7D-BF2221D41218" ];then
echo "AD Binding is present"
elif [ "$line" = "1C94DAD1-5FC7-46CE-9E09-576841C15093" ];then
echo "Energy Saver is present"
elif [ "$line" = "A0E5B977-F0AF-44C9-8001-DA0511B702B8" ];then
echo "Finder is present"
elif [ "$line" = "5E9DE5BF-34E4-4A7F-AA29-461FB0631943" ];then
echo "FV2 Redirect is present"
elif [ "$line" = "9AE91C88-D1B2-4227-9E95-80F492DCAA11" ];then
echo "Login Window/Security and Privacy is present"
elif [ "$line" = "00000000-0000-0000-A000-4A414D460003" ];then
echo "MDM Profile is present"
elif [ "$line" = "5E85BBF0-3483-4C80-A1FC-70AF20F82E7C" ];then
echo "Restrictions is present"
elif [ "$line" = "E433D546-5502-4C3F-9E5F-4732ED1F0032" ];then
echo "SAC SUBCA-01 is present"
elif [ "$line" = "5C2AE16B-D4E9-4D15-B190-3CD7B28779E8" ];then
echo "SAC SUBCA-02 is present"
elif [ "$line" = "2C620A13-DF1E-4F6A-A32B-9FA3149F8A56" ];then
echo "SAC-CA-01 is present"
elif [ "$line" = "3B44AE14-E0CE-4621-BACF-1A9C3BA4A459" ];then
echo "Screensaver is present"
elif [ "$line" = "396A9D84-A9CA-4575-8D09-C9F054B76AF7" ];then
echo "Spotlight is present"
elif [ "$line" = "E0138F02-9A15-47BD-8CA5-7D1D0985A1A6" ];then
echo "Workday Corp is present"
fi
exit 0
done <<<"$profilesInstalled"
You need a space around the = in those tests. That first test will always pass as written.
You should also quote the "$line" variable expansion.
Unless you use $profilesInstalled somewhere else you don't need that variable at all and can just pipe the profiles pipeline to the while loop directly.
You can also replace grep in that pipeline with awk '/attribute/ {print $4}'.
Some "meta" remarks first:
Please don't change your original question substantially, as that can invalidate existing answers (such as Etan Reisner's helpful answer)
Instead, add later changes to the original question and mark the changes as such.
If a different (follow-up) question arises, ask it as a separate, new question.
Please learn how to format your code properly - it was done for you, and you managed to destroy that formatting again with your later edits.
Please read about how to provide an MCVE (a Minimal, Complete, and Verifiable Example).
Not doing these things:
makes it far less likely that you'll get the help you need.
makes your question and its answers less valuable to future readers.
Here's a cleaned-up version of your code:
# Helper function to determine a string's element index in an array.
# SYNOPSIS
# indexOf needle "${haystack[#]}"
# *Via stdout*, returns the zero-based index of a string element in an array of strings or -1, if not found.
# The *return code* indicates if the element was found or not.
indexOf() {
local e ndx=-1
for e in "${#:2}"; do (( ++ndx )); [[ "$e" == "$1" ]] && echo $ndx && return 0; done
echo '-1'; return 1
}
# Define array of profile IDs, and parallel ID of profile names.
# Note: in bash 4+, this could be handled more elegantly with a single
# associative array.
profileIds=( F2CC78D2-A63F-45CB-AE7D-BF2221D41218 1C94DAD1-5FC7-46CE-9E09-576841C15093
A0E5B977-F0AF-44C9-8001-DA0511B702B8 5E9DE5BF-34E4-4A7F-AA29-461FB0631943
9AE91C88-D1B2-4227-9E95-80F492DCAA11 00000000-0000-0000-A000-4A414D460003
5E85BBF0-3483-4C80-A1FC-70AF20F82E7C E433D546-5502-4C3F-9E5F-4732ED1F0032
5C2AE16B-D4E9-4D15-B190-3CD7B28779E8 2C620A13-DF1E-4F6A-A32B-9FA3149F8A56
3B44AE14-E0CE-4621-BACF-1A9C3BA4A459 396A9D84-A9CA-4575-8D09-C9F054B76AF7
E0138F02-9A15-47BD-8CA5-7D1D0985A1A6 )
profileNames=( "AD Binding" "Energy Saver"
"Finder" "FV2 Redirect"
"Login Window/Security and Privacy" "MDM Profile"
"Restrictions" "SAC SUBCA-01"
"SAC SUBCA-02" "SAC-CA-01"
"Screensaver" "Spotlight"
"Workday Corp" )
# Feeding the list of installed profile IDs via a process
# substitution (<(...)), loop over them and print their
# respective names.
while read -r line ; do
# Find the line in the array of profile IDs and
# print the corresponding name.
if ndx=$(indexOf "$line" "${profileIds[#]}"); then
echo "${profileNames[ndx]} is present"
else
echo "WARNING: Unknown profile: $line" >&2
fi
done < <(profiles -P | awk '/attribute/ {print $4}')
As for why your code didn't loop:
You have an unconditional exit 0 statement in your loop, which means that the loop is always exited after the 1st line.
Due to using <<< to feed the list of profiles, you always get at least 1 line of input, because <<< appends a trailing newline to its input. If the input is empty, you'll get one iteration with an empty line.
As for this message:
; exit;
logout
[Process completed]
It tells me two things:
You're on OSX, and you ran the script from Finder.
The script produced no output (if there had been output, it would have printed between the exit; and logout lines).
When you run a script from Finder, the shell that is used to run the script exits after the script has run - and whether its Terminal window stays open or not depends on your Terminal preferences - in your case, the window stays open, but since the shell has exited, you can't interact with it anymore.
Either way, your particular script should yield the same output, whether it is run from Terminal directly, or via Finder.