Tagged: bash Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 10:06 am on December 2, 2016 Permalink | Reply
    Tags: bash   

    Categories: Linux ( 89 )

    How to print the current date and time in BASH shell scripts 

    Sometimes it’s useful to print the current time and date in a BASH script. We can make use of the date command for that. By default, and if called without any parameters, it’ll print something like this:

    echo $(date)
    Tue 29 Nov 2016 23:08:10 EST

    We can shorten this to just the date by using a formatting shortcut like this:

    echo $(date +"%x")
    29/11/2016

    or just the time using this format:

    echo $(date +"%r")
    11:09:26 pm

    Formatting shortcuts can also be used together, like so:

    $(date +"%x %r")
    29/11/2016 11:03:44 pm

    For a complete list of shortcuts, try “man date” from the command line.





     
  • Jay Versluis 10:52 am on December 1, 2016 Permalink | Reply
    Tags: bash   

    Categories: Linux ( 89 )

    How to use functions in a BASH shell script 

    BASH can deal with simple functions, and they are defined like this:

    # this defines the function
    function testing {
        echo "Hi there!"
    }
    
    # this calls our function
    testing

    As far as I know, BASH functions cannot take or return parameters.





     
  • Jay Versluis 10:45 am on November 30, 2016 Permalink | Reply
    Tags: bash   

    Categories: Linux ( 89 )

    How to use variables in a BASH shell script 

    Here’s how to use simple variables in BASH shell scripts. It appears there are no data types, and everything’s a string (correct me if I’m wrong). We can define a variable by first setting it to a value, then later refer to that value with a dollar sign in front of the variable name.

    Here’s an example:

    #!/bin/bash
    
    VARIABLE="Testing"
    echo $VARIABLE

    Note that there are no spaces between the variable name, the equal sign or the value. Adding those will result in a runtime error.

    Variables can be defined in upper or lower case letters, or a combination thereof.

    BASH Variables have a global scope, unless they are prefaced with the local keyword inside functions (in which case, only said function will have access to its value).





     
  • Jay Versluis 8:45 am on March 7, 2015 Permalink | Reply
    Tags: bash,   

    Categories: Linux ( 89 )

    How to specify FTP credentials in command line scripts 

    It’s easy to establish an FTP connection using the ftp command from the Linux Command Line. Sadly this command does not accept login credentials as parameters – which means that if we use it in a script, our script will pause and wait for us to type those credentials in manually. Not really suitable for automated backups.

    Thanks to a clever mechanism called netrc we can create a file in the home directory of the user who runs the script and provide credentials there. Let me show you how this works.

    First we create a file called .netrc. It’s a hidden file and it needs to reside in the home directory of the user who will connect via FTP. I’m going to use root for this:

    vi ~/.netrc
    
    # machine  login  password 
    machine ftp.domain.com login yourusername password yourpassword
    

    The first line is just a comment to you can remember how to add parameters here. The second line is an example of a host you want to connect to. Add as many other servers as you like, all following the same pattern.

    Be aware that you need to connect to the server as it is specified in the .netrc file. In the above example, if you would connect to domain.com instead, you would be asked for credentials as netrc cannot find a match.

    The .netrc file needs to be readable only by this one user, otherwise connections may fail. We do this by changing the file permissions to 600:

    chmod 600 ~/.netrc
    

    That should do it! Try to connect with

    ftp ftp.domain.com
    

    and the connection will be established without the prompt for credentials.

    If netrc isn’t working for you, or you choose not to use it, note that you can also provide FTP credentials with a here script. I find that approach a bit clunky, but the following link has details on how to do that:





     
  • Jay Versluis 12:32 pm on February 20, 2014 Permalink | Reply
    Tags: bash, shell   

    Categories: Bookmarks ( 18 )

    Crash Course in BASH Script Programming (a classic):





     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel