1

issue related to this: Cannot make bash script work from cloud-init

I tried all kinds of variants like this:

function ge() {
    if [ "$1" == ""]
    then
        geany &
    else
        eval "geany $1 &"
#also tried:
        geany $1 &
        geany "$1" &
        etc
    fi
}

I tried with or without eval, with $1 quoted or not etc. In all cases (if it works at all) I get bash: [: some.txt: unary operator expected

What I want is that the editor opens/creates the file in the background, so I can still use the terminal for foreground tasks.

Ultimately, I want a working function that does what I intended above, but with geany replaced by $EDITOR. So on different platforms I can use a different editor.

Why is the syntax in functions different than in scripts?

user2943111
  • 421
  • 1
  • 5
  • 15
  • Do you want to start an interactive command, like an editor, in the background? It seems a little backwards. – Ted Lyngmo Jun 23 '20 at 10:49
  • Hi, I was adding aliases in ./bashrc, but for commands that take parameters and not just at the end (because of the &), a function seems appropriate. At work they use gedit or nano and no other editors are installed. We are not allowed to install software. At home I have everything resembling notepad, like notepadqq, gedit, kate, kwrite, nano (little different), and also use Visual Code, QtCreator or CodeLite... I copied the same aliases and functions to work server. Now all I have to do is change $EDITOR evironment variable on both platforms (work, home). – user2943111 Jun 24 '20 at 11:18
  • Aha, yes, that makes sense I guess. – Ted Lyngmo Jun 24 '20 at 11:23

1 Answers1

0

It's certainly possible to start commands in the background via a script:

#!/bin/bash

cmd=geany

function ge {
    if [[ $# -eq 0 ]]
    then
        ${cmd} &
    else
        ${cmd} "$@" &
    fi
}

ge "$@"

or simpler:

#!/bin/bash

geany "$@" &

but starting an interactive command in the background and terminating the script is likely to fail since the background command's stdin will be closed as soon as the script dies. You can wait for the background command to finish before terminating the script to fix that though.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    Hi, thanks. Testing for number of arguments with $# is a nice touch also. Then "$@" is even more flexible than just allowing for one parameter. BTW, the function comes in .bashrc. My function has the seemingly arbitrary name ge() since most frequently used text editors by me are either gedit or geany, so starting with ge on command line for an editor comes natural for me. Makes less sense for kate, kwrite, nano, vim... A more generic short name would be 'ed' – user2943111 Jun 24 '20 at 11:11
  • @user2943111 You're welcome. Yes `"$@"` is bash magic and will keep arguments containing spaces etc. sparated so `./scriptname 'first argument' 'number two'` will be passed as two arguments to `ge` (or `geany` in the simpler example). – Ted Lyngmo Jun 24 '20 at 11:14