Fedora Linux Support Community & Resources Center
  #1  
Old 15th May 2013, 12:47 PM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 37
Posts: 4,277
linuxfirefox
[BASH]: sed with a selection of an array fails, regex?

Heyas

Just found an old and forgotten function named CRON_Remove.
Played a bit with it, and got it so far, that one can select a line provided by $(crontab -l).

However, i seem to have issues with sed, even when copying working lines...

Screen:
Code:
# |                                                CRON: Remove jobs of ROOT                                                | #
# |                                                                                                                         | #
DEBUG 0 --->@monthly	for user in ;do /bin/rm -f /home//.backgrounds/*;done<---
DEBUG 1 --->@monthly	/usr/share/script-tools/Scripts-in-bin/qbkup<---
@monthly	for user in ;do /bin/rm -f /home//.backgrounds/*;done
@monthly	/usr/share/script-tools/Scripts-in-bin/qbkup
# |                                                                                                                         | #
# | Which Cron Job do you want to remove?                                                                                   | #
1) @monthly	for user in ;do /bin/rm -f /home//.backgrounds/*;done
2) @monthly	/usr/share/script-tools/Scripts-in-bin/qbkup
#? 1
# | Removing "@monthly for user in..."                                                                             [ WORK ] | #
sed: -e Ausdruck #1, Zeichen 33: Unbekannte Option für `s'
# | Removed "@monthly for user in..."                                                                              [ FAIL ] | #
#? 1
+ ~ $
The according code looks like:
Code:
	CRON_Remove() { # 
	# Remove a cronjob for current user
	#
		this="$CRON_Current"  ; [ -f $this ] && rm -f $this
		lines[$@]="" ; C=0
		isRoot && user="ROOT" || user="$USER"
		
		sT "CRON: Remove jobs of $user"
		
		# create tempfile & fill array
		crontab -l	> 	"$this"
		sed s/":"/" "/g -i "$this"
		while read line;do
			lines[$C]="$line"
			((C++))
		done < $this
		C=$[ $C - 1 ]
		
		# Verify :: only during writing / debug
		sE ; for num in $(seq 0 $C);do echo "DEBUG $num --->${lines[$num]}<---"; done ; crontab -l;sE

		# Select
		sE "Which Cron Job do you want to remove?"
		select remover in "${lines[@]}" ;do 
			[ $remover = Abort ] && retval=4 && break
			sP "Removing \"$(echo ${remover:0:20})...\"" "$PROGRESS"
			sed  s/"$remover"/"\r"/g -i "$this" ; 	retval=$?
			break
		done
		
		# Exit
		ReportStatus $retval "Removed \"$(echo ${remover:0:20})...\""
		return $retval
	}
I assume the source of this error to be some REGEX (or code) occouring in the selected line "$remover".
Any ideas or hints what i may do or should search?

Thank you in advance and have a nice day.
Regards sea

---------- Post added at 11:47 AM ---------- Previous post was at 09:36 AM ----------

Ok sorry, since one wants to test what the other has issues with, here's the 'runable' thing:

Screen:
Code:
:) ~ $ sudo sh tmp
Saving file
Fill array
Select...
1) Abort
2) @monthly	for user in ;do /bin/rm -f /home//.backgrounds/*;done
3) @monthly	/usr/share/script-tools/Scripts-in-bin/qbkup
#? 2
Removing "@monthly for user in..."
sed: -e Ausdruck #1, Zeichen 33: Unbekannte Option für `s'
doh ~ $
Code:
Code:
#!/bin/bash
this=/tmp/cron.tmp
lines[$@]=""
C=0

echo "Saving file"
crontab -l	> 	"$this"
sed s/":"/" "/g -i "$this"

echo "Fill array"
while read line;do
	lines[$C]="$line"
	((C++))
done < $this

echo "Select..."
select remover in Abort "${lines[@]}" ; do 
	[ "$remover" = Abort ] && retval=1 && break
	echo "Removing \"$(echo ${remover:0:20})...\"" 
	sed s/"$remover"/" "/g -i "$this"
	retval=$?
	break
done
exit $retval
__________________
EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

Last edited by sea; 15th May 2013 at 12:39 PM.
Reply With Quote
  #2  
Old 15th May 2013, 12:53 PM
ocratato Offline
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 2,650
linuxfirefox
Re: [BASH]: sed with a selection of an array fails, regex?

Yes, $remover is being expanded and the / characters are being treated as the end of the pattern.

It might be better to use
Code:
grep -v -F "$remover" $this
__________________
Has anyone seriously considered that it might be turtles all the way down?
That's very old fashioned thinking.
The current model is that it's holographic nested virtualities of turtles, all the way down.
Reply With Quote
  #3  
Old 15th May 2013, 01:43 PM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 37
Posts: 4,277
linuxfirefox
Re: [BASH]: sed with a selection of an array fails, regex?

OK figured, i could unset the array entry, and then write each array item as a new line into a file...

Code:
# Check which index
		toRemove=true
		select remover in "${lines[@]}" ;do 
			[ "$remover" = Abort ] && retval=4 && break
			sP "Removing \"$(echo ${remover:0:20})...\"" "$PROGRESS"
			for num in $(seq 0 $C);do
				if [ "$remover" = "$(echo -e "${lines[$num]}")" ]
				then	# remove from array
					[ true = $toRemove ] && \
						unset lines[$num] && \
						retval=$? && \
						toRemove=false
				fi
			done
			break
		done

# Write new file
		count=0
		if [ $retval -eq 0 ]
		then	[ -f "$this" ] && rm -f "$this"
			
			while [ $count -lt $[ $C + 1 ] ]
			do	[ ! "" = "$(echo ${lines[$count]})" ] && \
					printf "${lines[$count]}\n" >> "$this" 
				((count++))				
			done
			crontab "$this" && retval=$?
		fi
This one solved it the array way, still improveable

EDIT:
Sorry too unused with arrays still.
Thank you anyway ocratato, but that grep just listed the selected entry another time.
__________________
EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

Last edited by sea; 15th May 2013 at 02:00 PM.
Reply With Quote
  #4  
Old 15th May 2013, 02:37 PM
ocratato Offline
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 2,650
linuxfirefox
Re: [BASH]: sed with a selection of an array fails, regex?

Quote:
Thank you anyway ocratato, but that grep just listed the selected entry another time.
Perhaps you left out the -v option which should return everything but the match.
__________________
Has anyone seriously considered that it might be turtles all the way down?
That's very old fashioned thinking.
The current model is that it's holographic nested virtualities of turtles, all the way down.
Reply With Quote
  #5  
Old 15th May 2013, 03:07 PM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 37
Posts: 4,277
linuxfirefox
Re: [BASH]: sed with a selection of an array fails, regex?

Yes i did, actualy i copied the line twice Sorry.
No idea why it (that line) got shown 2 times though...

Grep works just as well, even less code..
But since i just spent some hours writing this and learning a little about array handling, i'll leave it this way.

Hope i recall the simpler way later:
Code:
grep -v -F "$remover" $this
__________________
EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

Last edited by sea; 15th May 2013 at 04:27 PM.
Reply With Quote
Reply

Tags
array , bash , fails , regex , sed , selection

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash / sed to clean up filenames techsupport Programming & Packaging 2 10th March 2013 12:44 AM
[SOLVED] [BASH]: Issue with sed to change grub theme. sea Programming & Packaging 7 25th December 2012 03:07 AM
[SOLVED] Bash Test RegEx pnelsonsr Using Fedora 0 13th December 2012 02:04 AM
sed regex string replacement after match string issue adosch Using Fedora 10 29th June 2007 04:07 PM
[bash-script] regex variable carramba Programming & Packaging 1 1st July 2006 05:46 PM


Current GMT-time: 14:01 (Saturday, 21-10-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat