1

I would like to create a bash script to choose from .ics files in a folder then do a 'find and replace' within that selected file, then save and rename that file. I have cobbled together some things from searches, but don't know enough about what I'm doing to make it all work...

Here is what I have for a menu:

    #!/bin/bash

echo "The following `*.ics` files were found; select one:"

# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'stop' to cancel: "

# allow the user to choose a file
select filename in *.ics
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == stop ]]; then break; fi

# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
    echo "'$REPLY' is not a valid number"
    continue
fi

# now we can use the selected file
echo "$filename installed"

# it'll ask for another unless we leave the loop
break
done

This is the code I currently use to edit the .ics files, but it changes ALL .ics files in the current directory:

#!/bin/bash

###Fixes all .ics files to give ALL DAY events rather than 0000-2359  
####All .ics files
FILES="*.ics"


# for loop read each file
for f in $FILES
do
INF="$f"

##Change DTSTART:[yyyymmddThhmmss] to DTSTART;VALUE=DATE...##

sed -i[org] 's/DTSTART:2016/DTSTART;VALUE=DATE:2016/g' $INF

sed -i[org] 's/T[0-9][0-9][0-9][0-9][0-9][0-9]/ /g' $INF

###Remove DTEND:###
sed -i[org] 's/DTEND:[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ /g'$INF

done

How can I put this all together to achieve my goal??

1

You could maybe structure it using a case construct, like this:

#!/bin/bash

shopt -s nullglob

echo 'The following `*.ics` files were found; select one:'

select f in *.ics "Quit"; do
  case $f in
    "Quit")
      echo "Quitting"
      break
      ;;
    *)
      if [ -z "$f" ]; then
        echo "Invalid menu selection"
      else
        echo "Doing something with $f"
      fi
      ;;
    esac
done

Change the echo "Doing something with $f" to do whatever you want with the selected file - if it's relatively complex, I'd suggest moving it to a shell function. Remember to quote it i.e. "$f" to prevent word splitting.

| improve this answer | |
  • Thanks for the help. Unfortunately I know just enough to be dangerous ;) Could you give an example of where to place my "sed" lines, or where I would place the "shell function"? – Chad Wright Sep 23 '16 at 13:47
0

Okay, I figured it out. Here is my code:

#!/bin/bash


shopt -s nullglob

echo 'The following `*.ics` files were found; select one:'

select f in *.ics "Quit"; do
  case $f in
    "Quit")
      echo "Quitting"
      break
      ;;
    *)
      if [ -z "$f" ]; then
        echo "Invalid menu selection"
      else
        echo "Creating all-day events in $f"
          ##Change DTSTART:[yyyymmddThhmmss] to DTSTART;VALUE=DATE...##
          sed -i.orig 's/DTSTART:2016/DTSTART;VALUE=DATE:2016/g' "$f"
          sed -i.orig 's/T[0-9][0-9][0-9][0-9][0-9][0-9]/ /g' "$f"
          ###Remove DTEND:###
          sed -i.orig  's/DTEND:[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ /g' "$f"
      fi
      ;;
    esac
done

Is there a way to repeat the menu after either a valid or invalid response? (Sorry, I'm a bit of a newbie at this bash stuff, trying to learn as I go.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.