Results 1 to 5 of 5

Thread: script with sudo sh

  1. #1
    Join Date
    Jul 2013
    Beans
    2

    script with sudo sh

    Hi,

    I'm a beginner in linux so excuse me if my question is too easy.

    I want to make a script to install a program (ROS) and I need to write this line:
    Code:
     sudo sh -c 'echo "TEXT VARIABLE TEXT" > systemFile'  # to write in systemaFile I need sudo sh
    If echo is just fixed text, it works.
    If echo is text + variable it doesn't work. The variable is the codename of my linux version (in 12.10 is "quantal").

    I've tried with:



    Code:
    read f1 < <(lsb_release -a | grep Code* | cut  -f2)   #codename is writted in variable $f1
    echo $f1 # retruns "quantal" as I expected
    sudo sh -c 'echo "TEXT $f1 TEXT" > systemFile'  #f1 is empty, WHY?

    Then I have to assign the variable inside the same instruction sudo sh, for example:
    Code:
    sudo sh -c ' read f1 < <(lsb_release -a | grep Code* | cut  -f2) ; echo "TEXT $f1 TEXT" > systemFile' 
    
     sh: 1: Syntax error: redirection unexpected

    The error is in the first instruction of sudo sh (read f1 < <(lsb_release -a | grep Code* | cut -f2)), executed without sudo sh it works, but inside sudo sh -c it doesn't work.

    Anyone knows why? How can I do this?
    Thanks in advance

  2. #2
    Join Date
    Aug 2011
    Location
    52.5° N 6.4° E
    Beans
    6,826
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: script with sudo sh

    The shell you start with sh -c doesn't inherit the variables you set in your first shell:
    Code:
    $ f=hello
    $ echo $f
    hello
    $ bash
    $ echo $f
    #Variable not defined
    $ exit
    exit
    $ echo $f
    hello
    This can be fixed using export (or setenv, depending on your shell), turning it into an environment variable:
    Code:
    $ export f=hello
    $ echo $f
    hello
    $ bash
    $ echo $f
    hello #Variable defined
    $ sudo echo $f
    [sudo] password for user: 
    hello
    $ exit
    exit
    Also, it may be easier not to use sudo in a script, but call the entire script with sudo.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: script with sudo sh

    Simple answer - single quotes prevent shell expansion i.e. everything within single quotes gets interpreted as a literal string

    You could backslash-escape the inner quotes e.g.

    Code:
    $ f1="something" 
    $ sh -c 'echo "TEXT $f1 TEXT"'
    TEXT  TEXT
    $
    $ sh -c "echo \"TEXT $f1 TEXT\""
    TEXT something TEXT
    $
    or for your particular case (echoing a line to a system file) a better solution might be

    Code:
    echo "TEXT VARIABLE TEXT" | sudo tee systemFile
    but probably the best way around this is to execute the whole install script with sudo rather than having multiple individual 'sudo sh -c ...' subshells

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: script with sudo sh

    you don't have to grep anything to get the codename, lsb_release supports -c (codename) and -s (short version)

    Code:
    $ lsb_release -h
    Usage: lsb_release [options]
    
    Options:
      -h, --help         show this help message and exit
      -v, --version      show LSB modules this system supports
      -i, --id           show distributor ID
      -d, --description  show description of this distribution
      -r, --release      show release number of this distribution
      -c, --codename     show code name of this distribution
      -a, --all          show all of the above information
      -s, --short        show requested information in short format
    Code:
    $ lsb_release -sc
    lucid
    $ sh -c "echo 123 $(lsb_release -sc) 456"
    123 lucid 456
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: script with sudo sh

    +1 for Vaphell,

    Thats how I would have done it... Assuming your example is the exact script you are running.

    Also +1 for steeldriver,

    In a second case I would rather go with his/her recommendation:

    echo "TEXT VARIABLE TEXT" | sudo tee systemFile
    Cheers
    Switched away from windows XP to Ubuntu 9.04. Never turned around to look back.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •