FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    37
    Posts
    4,277

    [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
    Last edited by sea; 15th May 2013 at 12:39 PM.
    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

  2. #2
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,708

    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

  3. #3
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    37
    Posts
    4,277

    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.
    Last edited by sea; 15th May 2013 at 02:00 PM.
    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

  4. #4
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,708

    Re: [BASH]: sed with a selection of an array fails, regex?

    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.

  5. #5
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    37
    Posts
    4,277

    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
    Last edited by sea; 15th May 2013 at 04:27 PM.
    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

Similar Threads

  1. bash / sed to clean up filenames
    By techsupport in forum Programming & Packaging
    Replies: 2
    Last Post: 10th March 2013, 12:44 AM
  2. [SOLVED] [BASH]: Issue with sed to change grub theme.
    By sea in forum Programming & Packaging
    Replies: 7
    Last Post: 25th December 2012, 03:07 AM
  3. [SOLVED] Bash Test RegEx
    By pnelsonsr in forum Using Fedora
    Replies: 0
    Last Post: 13th December 2012, 02:04 AM
  4. Replies: 10
    Last Post: 29th June 2007, 04:07 PM
  5. [bash-script] regex variable
    By carramba in forum Programming & Packaging
    Replies: 1
    Last Post: 1st July 2006, 05:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •