Emulating a do-while loop in Bash

Two simple solutions: Execute your code once before the while loopactions() { check_if_file_present # Do other stuff } actions #1st execution while [ current_time <= $cutoff ]; do actions # Loop execution done Or:while : ; do actions [[ current_time <= $cutoff ]] || break done

Open and write data to text file using Bash?

The short answer: However, echo doesn’t deal with end of line characters (EOFs) in an ideal way. So, if you’re gonna append more than one line, do it with printf: The >> and > operators are very useful for redirecting output of commands, they work with multiple other bash commands.

Is bash a programming language?

We can say that yes, it is a programming language. According to man bash, Bash is a “sh-compatible command language”. Then, we can say a “command language” is “a programming language through which a user communicates with the operating system or an application”. From man bash: DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from … Read more

Return value in a Bash function

Although Bash has a return statement, the only thing you can specify with it is the function’s own exit status (a value between 0 and 255, 0 meaning “success”). So return is not what you want. You might want to convert your return statement to an echo statement – that way your function output could be captured using $() braces, which seems to be exactly what you want. Here is … Read more

What is a list in Bash?

I have searched for a similar question here, but surprisingly could not find any. In GNU bash, there is (a construct? a structure? a data type?) called “arrays“. Arrays are well documented in the bash documentation, so I think that I understand the basics. But suddenly, in the documentation there also comes up the term … Read more

How can I compare numbers in Bash?

In Bash, you should do your check in an arithmetic context: For POSIX shells that don’t support (()), you can use -lt and -gt. You can get a full list of comparison operators with help test or man test.

Using the star sign in grep

The asterisk is just a repetition operator, but you need to tell it what you repeat. /*abc*/ matches a string containing ab and zero or more c’s (because the second * is on the c; the first is meaningless because there’s nothing for it to repeat). If you want to match anything, you need to say .* — the dot … Read more

How to trim whitespace from a Bash variable?

A simple answer is: Xargs will do the trimming for you. It’s one command/program, no parameters, returns the trimmed string, easy as that! Note: this doesn’t remove all internal spaces so “foo bar” stays the same; it does NOT become “foobar”. However, multiple spaces will be condensed to single spaces, so “foo bar” will become “foo bar”. In addition it doesn’t remove … Read more

Using find command in bash script

Welcome to bash. It’s an old, dark and mysterious thing, capable of great magic. 🙂 The option you’re asking about is for the find command though, not for bash. From your command line, you can man find to see the options. The one you’re looking for is -o for “or”: That said … Don’t do this. Storage like this may work for simple … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)