error comparing strings
I am building a script to manually update my dovecot packages directly from dovecot repo , however this script does not do that job straight ahead .
First checks apt policy to see witch packages of dovecot are installed in system , and then write those packages names to a file . I am posting here this part only of the code , but when i look into the logfile from apt and compare strings with IF statement i get "invalid arithmetic operator on my loop." Code:
#!/bin/bash |
If you mean this line:
Code:
if [[ "$rdinst" -ne "$rjct" ]] And it would be != |
The != is the correct operator to compare strings. You might use "set -x" or "set -xv" earlier in the script and watch what it tells you about the process.
Some random comments. Making a directory and subdirectory can be done in a single move: Code:
mkdir -p "$basedir/update" Code:
. . . |
Code:
if [[ ! -d "$basedir" ]] Code:
if [[ ! -d "$basedir" ]] this here Code:
if [[ "$rdinst" -ne "$rjct" ]] example Code:
#!/usr/bin/env bash as @Turbocapitalist posted ahead of me, that awk he is doing is prob best to get what info out of the file that is needed to be checked against. |
Quote:
Code:
head -n1 "$f1" |
Quote:
Code:
#!/usr/bin/env bash |
Also, regarding the mkdir/rm stuff... since -f and -p suppress errors regarding (non-)existence I think the whole if/else might not be needed.
Depending on the specifics of what should/shouldn't be deleted, perhaps one of these is suitable: Code:
mkdir -p "$basedir/update" Code:
[[ -d "$basedir" ]] && rm -rf "$basedir/*" |
Just a comment:
Code:
IFS= read -r line1 <file1 |
keeping in mind you need to add the -r (for recursive) if going past parent dir.
Code:
userx@FreeBSD12.1.org:~ |
Quote:
Quote:
|
Thanks for all the replies .
thanks for some tips . I am not comparing files , what i am doing is to see in "Installed" field , pick whats next "var2" , and if output is different than "(none)" then pick up the name of that installed package only to a file . example : when loop gets to dovecot-pigeonhole-dbg: it reads the next line "Installed" , then grabs "2:2.3.9.2-1+debian10" and compare with "(none)" . In case it is different then writes "dovecot-pigeonhole-dbg:" to a file . I am pretty sure there is a simple way to do this , and i have faith that someone here already figured out to do it in a single line ;) . Quote:
|
Quote:
Code:
grep -Poz '(?<=\n|\A)dovecot.*:(?=\nInstalled: (?!\(none\)\n))' packages.txt Uses -P for Perl regex to get lookahead support, with -z to match across lines, and -o to only output the matched text. The "(?<=\n|\A)" is start of line/content - it would ideally just be "^" but that's not supported with the -z flag. The "dovecot.*" searches for all dovecot prefixed packages - if you need specific ones, replace it with "(package1|package2)" The next bit looks for "\nInstalled: " that isn't followed by "(none)\n" - using a lookahead to not include it in the output. This version does include the colon in the output, but if you don't actually want that, move the : inside the lookahead (i.e. just before the \n ) Assumes the "Installed:" line always directly follows the package name, which seems a safe assumption? If not, (and depending on exact format possibilities), I might filter the content first to remove other lines. |
Quote:
In this file (aptinst), how to see witch packages are installed automatically without adding anything specifically to the script ? Quote:
For this specific file i need this outputs : Quote:
This is why i cant specify a specific package to grep . And this is why i need to look into each package "Installed" field to see if have "(none)" or is different . |
Quote:
Code:
$ grep -Poz '(?<=\n|\A)dovecot.*:(?=\nInstalled: (?!\(none\)\n))' packages.txt You can even remove "dovecot" (leaving just the ".*" part) and it will continue to work even with package names not starting with dovecot (that might not be required, just pointing out the potential). |
dont know why but it is not working here .
what is your grep version ? mine is 3.3 . I have no idea if this is a grep version , but it is strange . |
All times are GMT -5. The time now is 01:44 AM. |