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

Thread: Where do I put scripts to make them work?

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

    Where do I put scripts to make them work?

    I'm a total noob to scripts and looking for your help, please.

    I've written my first script, which looks like this:
    Code:
    #! /bin/bash
    sudo apt update && sudo apt upgrade && sudo apt autoremove
    The script file is right now:
    Code:
    /home/macro/upgrade
    Permissions are OK (bits set to -rwxr-x--x)

    But where should I place the script file?
    At this point it doesn't work, as the shell can't find it.
    Should I move it to somewhere else?
    Or do I need to set an environment variable?

    I'm lost.
    Last edited by GhX6GZMB; September 30th, 2021 at 11:28 PM.

  2. #2
    Join Date
    Dec 2014
    Beans
    2,584

    Re: Where do I put scripts to make them work?

    If you enter just the name of a program into the shell it searches for the program file in the directories listed in the variable PATH. Enter 'echo $PATH' into a shell to see what directories are in PATH.

    If you give the shell a path and a file name, then it will execute a program even if it's not in a directory that's part of PATH. So if you enter '/home/macro/upgrade' into the shell (or './upgrade' if /home/macro/ is your current working directory) it will call your script.

    Alternatively there are two directories which - if they exist - are automatically added to PATH: '~/bin/' (which AFAIK is not created by default for a user, so you'd have to create it and possibly restart at the very least your shell so it picks up on the existence of the directory) and '~/.local/bin/' (which I think does exist by default ...). So if you move your script into one of these directories you should be able to call it without explicitly giving a path to it.

    And of course you could change PATH to include a directory of your choice (entering 'PATH=${PATH}:${HOME}/myscripts' into the shell would add the directory ~/myscripts at the end of the PATH for the duration of your current session, so it would become the last directory to search for programs; to change the PATH permanently edit ~/.profile and add that command or change a command setting the PATH to include your changes).

    Holger

  3. #3
    Join Date
    Jul 2010
    Location
    ozarks, Arkansas, USA
    Beans
    14,199
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: Where do I put scripts to make them work?

    ml9104; Hello

    I expect that your need has been foreseen by our developers

    Does the directory 'bin' already exist in your /home ?
    ls -al /home/sysop/ >> drwxrwxr-x 3 sysop sysop 4096 Aug 11 2020 bin
    where my username on my system is "sysop"

    no new wheel here
    THE current(cy) in Documentation:
    https://help.ubuntu.com/community/PopularPages

    Happy ubuntu'n !

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

    Re: Where do I put scripts to make them work?

    @Holger_Gehrke:
    Thanks for your insight, running the 'echo $PATH' was really helpful.
    I've now placed my script in /usr/local/sbin, seeing that it needs a sudo password to work. It runs wonderfully. I've also changed the permissions to -rwx------ and ownership to "root".

    The references to a "bin" directory in $HOME I don't understand
    .
    PS: edited to correct a couple of mistakes.
    Last edited by GhX6GZMB; October 1st, 2021 at 11:32 PM.

  5. #5
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,701

    Re: Where do I put scripts to make them work?

    If you create a bin directory in your home directory (e.g. /home/macro/bin) then that directory will automatically be added to your PATH next time you log in. But of course, only your PATH will include that, not any other user's PATHs. I keep lots of utility scripts in my personal bin folder.

  6. #6
    Join Date
    May 2010
    Beans
    3,247

    Re: Where do I put scripts to make them work?

    You can put the file in any folder that is in your path variable. I lie to make a folder to hold all my scripts (personally).

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

    Re: Where do I put scripts to make them work?

    Quote Originally Posted by The Cog View Post
    If you create a bin directory in your home directory (e.g. /home/macro/bin) then that directory will automatically be added to your PATH next time you log in. But of course, only your PATH will include that, not any other user's PATHs. I keep lots of utility scripts in my personal bin folder.
    VERY interesting, Thank You. I'll try that as well.

  8. #8
    Join Date
    Jun 2007
    Location
    Arizona U.S.A.
    Beans
    5,739

    Re: Where do I put scripts to make them work?

    There's possibly already someplace for your scripts in your home folder among the locations in PATH for executable files. My current OS has ~/.local/bin by default. ~/.local/share is also searched for themes and icons and these are priority locations over themes and icons in /usr/share.
    Last edited by Dennis N; October 2nd, 2021 at 04:26 PM.

  9. #9
    Join Date
    Apr 2014
    Location
    Tucson AZ, USA
    Beans
    1,057
    Distro
    Ubuntu

    Re: Where do I put scripts to make them work?

    If I may. Something I didn't figure out till after awhile. You can run the entire script with a single sudo.

    Code:
    #!/bin/bash
    
    apt update
    apt upgrade
    apt --purge autoremove
    Execute via
    Code:
    sudo /path/to/script

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

    Re: Where do I put scripts to make them work?

    Quote Originally Posted by Tadaen_Sylvermane View Post
    If I may. Something I didn't figure out till after awhile. You can run the entire script with a single sudo.
    Yes, of course. That's the whole point.

Page 1 of 2 12 LastLast

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
  •