Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Bash

  1. #1
    Join Date
    Nov 2016
    Beans
    45

    Bash

    At my wits end with this one

    Trying to write a script that will not only work on my machine, but if shared too. In that regard, I would need to refer to the ~/ or home directory in a script. What I'm learning is that this is virtually impossible.

    I've tried:
    Code:
    myHome=~
    eval myHome=$myHome
    #echo $myHome

    But at the end of the day, it's not working. It will echo correctly to the screen, but I'm utterly unable to use these methods to construct a path to files in the home directory, and then cp files from ~/whatever to wherever, which is beyond frustrating.

    Last edited by yegnal; June 10th, 2020 at 11:34 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    Southern California, USA
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

  3. #3
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Bash

    Using whoami is good choice. The $() construct in bash tells the shell to execute the command it finds inside the parentheses and return the result as a string.

    Usually systems create "environmental variables" like $USER and $HOME when someone logs in.
    Code:
    seiji@Linux:~$ echo $USER
    seiji
    seiji@Linux:~$ echo $HOME
    /home/seiji
    Now if you're trying to write a script that would run correctly across all platforms and shells, that's way beyond my abilities. In fact, I'm not sure it's possible. Even on Linux, you can have different shells with different commands and environments. In scripts, it's always a good idea to specify bash explicitly like:
    Code:
    #!/bin/bash
    echo $USER
    echo $HOME
    You can see the entire array of default environment variables with the "printenv" command.
    Last edited by SeijiSensei; June 10th, 2020 at 11:55 PM.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  4. #4
    GhX6GZMB is offline Iced Almond Soy Ubuntu, No Foam
    Join Date
    Jun 2019
    Beans
    1,093

    Re: Bash

    NEVER!!! use ~ in a script. It's a convenient shortcut on some keyboards, nothing else. Only a shell can work with ~, most other programs (including the Kernel) have no idea what it is.
    The correct way is to use $HOME (environment variable which ~ translates to). Always works.

    Cheers.
    Last edited by GhX6GZMB; June 10th, 2020 at 11:58 PM.

  5. #5
    Join Date
    Nov 2016
    Beans
    45

    Re: Bash

    When I run a test script
    Code:
    myHome=$(whoami)
    myPathVariable='/home/'$myHome'/Documents/'
    echo $myPathVariable
    I get /home/me/Documents/

    but when trying to copy I get
    cp: cannot stat '/home/root/Documents

    Where does the work root come in ? ! ?. The script is being run as SUDO, and I can make the copy of the file in a terminal directly no problem, just not working as a script......

  6. #6
    Join Date
    Nov 2016
    Beans
    45

    Re: Bash

    Code:
    myPathVariable=$HOME'/Documents/'
    echo's correctly in terminal, error as before trying to copy a file.

    Error is: cp: cannot stat '/home/root/Documents

    It's being run as sudo, can't figure what the issue is..

  7. #7
    Join Date
    Nov 2016
    Beans
    45

    Re: Bash

    So, a simple test using
    Code:
    $HOME'/Documents/'

    to copy a file works, I'm now guessing because I'm trying to copy to /usr/share/..... I'm getting some issue that even running as sudo is not allowing.

    Any guesses...

    The permissions are good, I think...

    -rwxrwxr-x 1 me me

    do I need to add a sticky bit or something ?

  8. #8
    GhX6GZMB is offline Iced Almond Soy Ubuntu, No Foam
    Join Date
    Jun 2019
    Beans
    1,093

    Re: Bash

    As no one knows what you're trying to do, just read post #4 again.

  9. #9
    GhX6GZMB is offline Iced Almond Soy Ubuntu, No Foam
    Join Date
    Jun 2019
    Beans
    1,093

    Re: Bash

    Why are you using quotes? Example: $HOME/Documents is the same as ~/Documents
    Last edited by GhX6GZMB; June 11th, 2020 at 12:26 AM.

  10. #10
    Join Date
    Dec 2014
    Beans
    2,590

    Re: Bash

    sudo means "switch user and do", but unless you tell it what user to switch to (by giving the option -u followed by a username) it will switch to the system administrator which on Unix-like systems is called root and has '/root/' as the home directory.

    Try this:
    Code:
    sudo bash -c 'echo $HOME'
    If this shows '/root' then either the security policy in /etc/sudoers is set to run sudo as if the -H option is in effect ('Defaults always_set_home') or sudo is aliased to 'sudo -H'. With the -H option sudo sets HOME to the home directory of the user to which you are switching.

Page 1 of 2 12 LastLast

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
  •