I have a script that starts a new activity for hamster (activity tracker). This is my current script:

ACTIVITY=`zenity --entry --title "Enter new activity name" --text "New activity name:"`
hamster start $ACTIVITY

The problem is I would like the script to remember the last task so I can just run it with ENTER instead of typing the name again. I want it to store the name of it in file. Currently I have made the file manually: /usr/local/bin/hamster-log.

My script so far is the following:

activity=$(zenity --entry --title "Enter new activity name" --text "New activity name:" --entry-text "$last")
hamster start "$activity"
if [ ! -z "$activity" ]; then
    if [ -f "$file" ]; then
        echo "$activity" > "$file"

The weird thing is when I run the script from terminal, it reads the file's content properly, but when I run it using a shortcut it doesn't read the file.

I have set the file's permissions to 777.


I'm not sure how you're calling the script in the custom shortcut but there are two possibilities:

  • You're calling an interpreter (e.g. sh /path/to/script): then you're calling sh or an incompatible shell: command substitutions such as $(<file) are replaced with the content of file only in Bash or compatible shells, so you may be calling an incompatible shell;
  • You're calling the script (e.g. /path/to/script): then the script doesn't have a shebang at the top.

So either:

  • If the first one, call the right interpreter (bash /path/to/script);
  • If the second one, add a shebang at the top of the script (#!/bin/bash).

Alternatively do something ugly (but POSIX and portable) such as:

last=$(cat "$file")
| improve this answer | |
  • Thanks. You are my hero. I have spent a lot of time trying different ways. BTW I was using the second way of calling the script. – Juraj.Lorinc Jan 14 '16 at 22:51
  • @Posuk13 Yeah it was either one or the other, so I posted both. I kind of suspected it since you didn't have a shebang in the script you posted. Glad that it helped. – kos Jan 14 '16 at 22:56

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.