S
S
Stas2016-02-01 11:58:16
ruby
Stas, 2016-02-01 11:58:16

Should I learn to write bash scripts?

Hello toaster. I am a web developer, I read a lot of different things in my free time.
After reading the literature about linux / unix, I recently came across tutorials about bash.
Making an analogy between bash and ruby, I don't see what can be done in a bash script that can't be done in ruby. On ruby, you can easily execute system commands through the system () function, you can run the script in the console, take the parameters entered by users through the ARGV array and etc.
I also sometimes heard that in some office, system administrators with knowledge of ruby ​​or python are required.
Therefore, the question arose: is it worth paying attention to writing scripts in bash now, or have languages ​​\u200b\u200bsuch as ruby ​​and python supplanted it?
If I'm wrong somewhere, please correct me.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey Nemiro, 2016-02-01
@stasretrovv

Definitely worth learning bash . Linux systems do a lot on it, so you'll have to deal with it anyway.
I didn't learn bash on purpose. At some point, I just took it and started writing code on it :-) I can say that the language is terrible, finicky, extremely inconvenient in places, oversaturated with pitfalls (more precisely, sea urchins and mines). But at the same time, bash is quite a powerful language.
For small scripts it will be easier to use bash . For something complex, it's better to use a normal programming language.
To understand the basics, good knowledge and experience with any other programming language will be enough; this will help you ask the right questions and find answers quickly. Simplicity is
at the core of the ideology of Unix -like systems. So something difficult is likely to be encountered rarely. Knowledge of programming, or even the basics of programming, should be enough to understand the bash scripts that you will encounter. But a couple of weeks to carefully pick it up, it won’t be superfluous, the main thing is to find motivation.

A
aminought, 2016-02-01
@aminought

It is not always possible to have a Ruby interpreter installed on a machine. So yes, it's worth it.

V
Vladimir, 2016-02-01
@azrail_dev

Hardcore scripts of 1000 lines are not worth writing and you should not learn this either, but bash is needed, it will come in handy. Sometimes a simple script of 30 - 50 lines saves a couple of hours of free time.

R
res2001, 2016-02-01
@res2001

In niks, some of the system functions are performed on bash scripts, they are there all the time. So at least you need to understand it.

A
abcd0x00, 2016-02-01
@abcd0x00

There are not so many scripts, they are somehow inconvenient to store and backup.
But there are many functions that are thrown into the initialization file.
And these are the ones that are used all the time.
When the external internet goes down

The code
# Print popup message
# Say(message, seconds)
Say()
{
    kdialog --passivepopup "$1" ${2:-3}
}

# Ping server until it wakes up
# Connect([site="www.yandex.ru"
#         [, count=1
#         [, interval=3
#         [, message="connected"]]]])
Connect()
{
    local site="www.yandex.ru"
    local count=1
    local interval=3
    local message="connected"

    [ "$1" = "--help" ] && {
        echo "usage: $FUNCNAME [site[, count[, interval[, message]]]]"
        echo "       $FUNCNAME $site $count $interval $message"
        return 1
    } 1>&2
    [ -n "$1" ] && site="$1"
    [ -n "$2" ] && count="$2"
    [ -n "$3" ] && interval="$3"
    [ -n "$4" ] && message="$4"

    while ! ping -c $count "$site"; do
        sleep $interval;
    done
    Say "$message"
}

When you need to download a clip from YouTube
The code
# Download YouTube video in selected format
# Ytf(url, ofile[, fmt=18])
Ytf()
{
    [ $# -eq 0 -o "$1" = "--help" ] && {
        echo "usage: $FUNCNAME url ofile [fmtn=18]"
        echo "       $FUNCNAME http://youtube 12345.flv"
        echo "       $FUNCNAME http://youtube 12345.flv 19"
        return 1
    } 1>&2
    n=${3:-18}
    youtube-dl -c -f "$n" "$1" -o "$2"
}

Sometimes you need to know the file size
The code
# Check access to url like a browser
# SpiderB(url[, agent])
SpiderB()
{
    agent=${2:-Wget}
    wget --spider --user-agent="$agent" "$1"
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question