linux - Is there a way to pass a statement via an argument in shell?

07
2014-07
  • phil pirozhkov

    What I'm trying to achieve is to set the title of a widget when computer goes to idle:

    xsidle.sh echo mywidget.title = "idle" | awesome-client
    

    when i remove xsidle.sh it works flawlessly. xsidle.sh is roughly:

    sleep 10
    command="$1"
    $command
    

    Of course the output of xsidle is redirected to awesome-client, while I'm trying to echo to awesome-client with a delay.

    I've tried escaping with " or ', the echo output is "mywidget.title = "idle" | awesome-client".

  • Answers
  • reinierpost
    #!/bin/sh
    sleep 10
    "$@"
    

    If you have nothing more to do after the command, you can use

    exec "$@"
    

    This is in your friendly manual page (try man sh), although it is not so easy to know how to find it. See also questions such as this one.


  • Related Question

    unix - shell dotfiles and *rcs: what's a sane setup?
  • kch

    A bash user will eventually end up with .bashrc, .bash_profile, .profile, and maybe some more.

    Now, each file gets loaded unders particular situations, and it all leads to confusion and frustration. I don't care about what shell is a login shell and neither should you.

    I just want to make sure the same thing is loaded for every shell thing that happens.

    So, what's the sane way to set them up?

    I'd wager non-bash-specific things go into .profile, and some file sources the others, etc. What exactly would in put in each to achieve an identical environment for every shell?

    Note: I'm not asking what you particularly enjoy putting in your rc files, like aliases and functions and so on. Just how you lay them out so as not to have things randomly spliced amongst them.


  • Related Answers
  • Jonathan Leffler

    I just want to make sure the same thing is loaded for every shell thing that happens.

    If you really want that, put everything in ~/.profile and add a source ~/.profile at the end of your ~/.bashrc. If this is desirable is a different question. To source ~/.profile in ~/.bashrc is a very common setup anyway.

    +------------+-----------------+--------------------+
    |            | login shells    | interactive shells |
    +------------+-----------------+--------------------|
    | all        | /etc/profile    |                    |
    | bourneish  | ----------------+--------------------|
    | shells     | ~/.profile      |                    |
    +------------+-----------------+--------------------|
    | just       | ~/.bash-profile | /etc/bash.bashrc   |
    |            | -------------------------------------|
    | bash       | ~/.bash-login   | ~/.bashrc          |
    +------------+-----------------+--------------------+
    

    C shell and shells derived use a different set of files (.login, .cshrc, ..).

    What exactly would in put in each to achieve an identical environment for every shell?

    If you mean environment in the sense of environment variables, just set all of them in ~/.profile and you are OK. If you mean environment in a broader sense, it depends.

    The issue here is that it is not desirable to have the very same environment for interactive and login shells. An example is aliases: Maybe you want aliases in your interactive shell, but very likely they will make your scripts do weird things. So you don't want your aliases in non-interactive shells => put them in ~./bashrc.