0

So I have the following script which will show me my bookmarks in dmenu then open the selected Item in firefox.

#!/bin/sh

declare -a bookmarks

bookmarks=(
https://www.reddit.com/
https://www.youtube.com/
https://github.com/
https://www.veganricha.com/
)

input="$2"
addnew() {
    bookmarks+=("$input")
}


while getopts a: option; do
    case "${option}" in
        a) addnew && exit 0 ;;
    esac
done

menu="dmenu -i -l 10 -p "Bookmarks""
items=$(printf '%s\n' "${bookmarks[@]}" | $menu )

[ -n "$items" ] && firefox $items || exit 0

when you run it with the -a flag, you can add an Item to the list (the last copied item in my case)

./bookmarks -a $(xclicp -o)

and it does indeed add the item. what I want is to write the changes to the script. so the newly added item using -a flag be written to the script.

I tried and searched a lot but couldn't figure it out, hope someone can help

Thank you in advance

1
  • 4
    I think the best is to put the bookmarks elements in a separate file, as they are data. Then the script reads that file into the bookmarks array and appends new entries to the file. Commented May 4, 2020 at 12:00

1 Answer 1

0

Instead of modifying the script itself it will probably be better to have the bookmarks in a separate file and modify that file instead like Quasimodo commented

You can do this by modifying the script to something like the following:

#!/bin/sh

bookmark_file=~/bookmark_list

declare -a bookmarks

bookmarks=($(<$bookmark_file))

input="$2"
addnew() {
    echo $input >> $bookmark_file
}


while getopts a: option; do
    case "${option}" in
        a) addnew && exit 0 ;;
    esac
done

menu="dmenu -i -l 10 -p "Bookmarks""
items=$(printf '%s\n' "${bookmarks[@]}" | $menu )

[ -n "$items" ] && firefox $items || exit 0

And have the bookmarks in a file at ~/bookmark_list, the file storing the bookmarks can be easily changed by changing the bookmark_file variable

1
  • while this answer is correct, and that's why I gave you an upvote, I can't mark it as accepted because it doesn't answer the question itself. Commented May 4, 2020 at 16:51

You must log in to answer this question.