LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2019, 11:53 AM   #16
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled

wait , got it working by copying my text from here to a new file in server .

interesting , this must be happening due to file properties .
 
Old 12-16-2019, 12:16 PM   #17
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Damn !!! can not get it to work locally .
Both files have same attribute and encoding , if i copy my text here to a new file on server and run your code then it works perfectly .
But if write any of these lines then it will not work :

Code:
apt-cache policy dovecot-* > aptinst
grep -Poz '(?<=\n|\A)dovecot.*:(?=\nInstalled: (?!\(none\)\n))' < aptinst > test
Code:
apt-cache policy dovecot-* | grep -Poz '(?<=\n|\A)dovecot.*:(?=\nInstalled: (?!\(none\)\n))' > test
Code:
cat aptinst | grep -Poz '(?<=\n|\A)dovecot.*:(?=\nInstalled: (?!\(none\)\n))' > test
anyone here have an idea why this is happening ?
 
Old 12-16-2019, 12:20 PM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
would be nice to explain what do you mean by that: "it will not work" (you can compare grep versions on your hosts, if that is the issue)

Last edited by pan64; 12-16-2019 at 12:43 PM.
 
Old 12-16-2019, 12:55 PM   #19
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
would be nice to explain what do you mean by that: "it will not work" (you can compare grep versions on your hosts, if that is the issue)
Do i need to repeat myself again ?
It works for you because you copied the text from the board to a file , that also works for me .
But if you generate that text from this command

apt-cache policy dovecot-* > aptinst

and the run the code on that file it will not work .
 
Old 12-16-2019, 12:56 PM   #20
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554
It looks like apt-cache policy dovecot-* is prefixing "Installed:" with two spaces - so after the \n add either two spaces or " *" to allow any number of spaces (including none).

Also, testing on grep 3.3 using ^ is supported, and the \n needs to be captured, so changed "(?<=\n|\A)" to "(^|\n)"

Updated command:
Code:
apt-cache policy dovecot-* | grep -Poz '(^|\n)dovecot.*:(?=\n *Installed: (?!\(none\)\n))' > test

Is "apt-cache policy dovecot-*" definitely the best way to get this info?

Using "dpkg-query --show dovecot-* | cut -f1" might be simpler, assuming it lists correct packages and is available on the target systems.


Last edited by boughtonp; 12-16-2019 at 01:04 PM.
 
1 members found this post helpful.
Old 12-16-2019, 01:08 PM   #21
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
thank you boughtonp

Quote:
apt-cache policy dovecot-* | grep -Poz '(^|\n)dovecot.*?=\n *Installed: (?!\(none\)\n))' > test
worked perfectly now .
dpkg command like you said will show you all dovecot packages installed and not installed .
But since the code for apt is working perfectly then there is no need to waste more effort on dpkg .

Thank you for this excellent work , you saved me a lot of code lines .
 
Old 12-16-2019, 04:03 PM   #22
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by BW-userx View Post
Code:
if [[ ! -d "$basedir" ]]
then
    mkdir -p "$basedir/update"
else
    rm -rf "$basedir/*
fi
And what happens if $basedir is not set correctly!?

I recommend avoiding all such constructs when scripting. At the very least use bash's set -u and always test that the path is what you expect it to be before using rm, not simply that it exists!

Last edited by astrogeek; 12-16-2019 at 04:05 PM.
 
Old 12-16-2019, 04:12 PM   #23
KenUnix
LQ Newbie
 
Registered: Dec 2019
Location: Pennsylvania
Distribution: linuxubuntu
Posts: 28

Rep: Reputation: 6
I don't understand

if [[ ! -d "$basedir" ]]
then
mkdir -p "$basedir/update"
else
rm -rf "$basedir/*
fi

If it does not exist create it. But if it does exist delete it?
Does not seem rite.

One method I use for testing strings and things see below

# Grep .bashrc looking for the string Linux putting result into temp.$$
grep "Linux" .bashrc >temp.$$
# Check size if temp.$$. Was match found?
if [ -s temp.$$ ]
then
# Yes
rm -f temp.$$
echo -e "==> .bashrc is already patched. Can not do it twice. <==\007"
echo " "
sleep 3
else
# No
rm -f temp.$$
# Look for PowerShell in path variable
# PowerShell appears when running under WSL (BASH)
# Not on a VM or when running stand alone
echo $PATH | grep "PowerShell" >temp.$$
# Was it found?
if [ -s temp.$$ ]
then
# Yes
echo "This appears to be running under WSL"
if [ ! -s "pathWSL" ]
then
echo "===> File pathWSL missing <==="
sleep 3
else
cat pathWSL >>.bashrc
fi
else
# No
echo "This appears to be runnitg under a VM or Standalone"
if [ ! -s "pathVM" ]
then
echo "===> File pathVM missing <==="
sleep 3
else
cat pathVM >>.bashrc
fi
# 'x' will not work on a VM so trash it
rm -f x
fi
fi
rm -f temp.$$


Ken
 
Old 12-16-2019, 04:15 PM   #24
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
And what happens if $basedir is not set correctly!?

I recommend avoiding all such constructs when scripting. At the very least use bash's set -u and always test that the path is what you expect it to be before using rm, not simply that it exists!
let me check
Code:
#!/usr/bin/env bash

basedir=mybaseNOTthereYo

if [[ ! -d $basedir ]] 
then
echo "make"
	mkdir -pv "$basedir"/hohoho
else
echo "remove"
	rm -rv "$basedir"/*
fi
results
Code:
$ ./whatdir
make
mybaseNOTthereYo
mybaseNOTthereYo/hohoho
userx@FreeBSD.efi.64.net:~
$ ./whatdir
remove
mybaseNOTthereYo/hohoho
if not there, make it there, if there, make it go away. why? I do not know, it is not my script. I just seen this
Code:
#!/bin/bash
basedir="/dovecot"
if [[ ! -d "$basedir" ]]
then
mkdir "$basedir"
mkdir "$basedir/update"
else
rm -f "$basedir/update/*.*"
rm "$basedir/dovelist"
rm "$basedir/filelist"
rm "$basedir/dovelist"
fi
then simplified it. again it is not my logic, not my script. Maybe OP wants a fresh start each run??? remove what is inside of it. Even though that is not a completed logical step.
what? now I got a figure that out ?

Code:
 
#if no base dir , then make it
#and make a sub dir
if [[ ! -d "$basedir" ]]
then
mkdir "$basedir"
mkdir "$basedir/update"

else

#else it is already there, so clean it out
#just incase something is in it for a fresh run

#remove files in that sub dir
rm -f "$basedir/update/*.*"

#remove that file or dir?
rm "$basedir/dovelist"

#remove that file or dir?
rm "$basedir/filelist"

#remove that file or dir?
rm "$basedir/dovelist"

fi
rm does not remove dir so it got a be a file.
if that is true then it could be written like this
Code:
#!/usr/bin/env bash

basedir=mybaseNOTthereYo

if [[ ! -d $basedir ]] 
then
echo "make"
	mkdir -pv "$basedir"/update
else
echo "remove"
	rm -rv "$basedir"/*
	mkdir -pv "$basedir"/update
fi
the later removes everything but the base dir in one command, then puts the sub dir back for the run.

Last edited by BW-userx; 12-16-2019 at 04:29 PM.
 
Old 12-16-2019, 04:38 PM   #25
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
And what happens if $basedir is not set correctly!?

I recommend avoiding all such constructs when scripting. At the very least use bash's set -u and always test that the path is what you expect it to be before using rm, not simply that it exists!
how can you test the path first before removing it, and not that it simply exists?

Code:
#!/bin/bash

Setpath=MyPath/dir

if [[ ! -d "$SetPath ]] ; 
then
  mkdir -p "$Setpath"
  mkdir -p "$Setpath"/anotherSubDir
else
  rm $Setpath/* 
fi
the var is used to make sure it is the path if that is wrong then open file before each use would be the only way to check it.... I do not get your logic on this one.
 
Old 12-16-2019, 04:55 PM   #26
KenUnix
LQ Newbie
 
Registered: Dec 2019
Location: Pennsylvania
Distribution: linuxubuntu
Posts: 28

Rep: Reputation: 6
#!/bin/bash

Setpath=MyPath

if [[ ! -d "$SetPath ]] ;
then
mkdir -p "$Setpath/dir"
mkdir -p "$Setpath/dir/anotherSubDir"
else
rm -v -r -f $Setpath
fi

Notice changes.

Ken
 
Old 12-16-2019, 05:24 PM   #27
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
jesus christ !!!! where does this goes , ehehehehe
wait a minute .
I dont want to delete all files from basedir , i only need to delete the temporary files , and this is why i used the rm to delete only some files .

Everything is working fine right now here .

By the way , i am just creating this script here because dovecot repository is not working correctly on sources.list .
for at least 2 months as i can remember that i get this error on apt from dovecot :
Quote:
The HTTP server sent an invalid reply header

W: The repository 'https://repo.dovecot.org/ce-2.3-latest/debian/buster buster Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: Failed to fetch https://repo.dovecot.org/ce-2.3-late...amd64/Packages The HTTP server sent an invalid reply header
E: Some index files failed to download. They have been ignored, or old ones used instead.
I have no idea if some of you have this issue , but i quit myself to try to figure what this problem is .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing binaries - excluding __DATE__ and __TIME__ strings bcg121 Linux - Software 2 12-16-2019 06:48 PM
problem with comparing strings in php Yoyo302 Programming 5 06-30-2009 04:16 AM
c++ - if/else problem comparing strings babag Programming 14 05-19-2008 11:32 PM
comparing 2 strings in shell script dhanabalanb Programming 3 08-01-2007 01:17 PM
Error comparing strings and acting on the comparison WindowsBurner Programming 4 10-21-2004 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration