scripting – Shell script to grep current PostgreSQL log

I have tried by google but unable to find.

I wanted to grep string from below no. of files which is most recent files.

by using this command i am able to fetch most recent file but , not able to grep in that file only.

ls -lrth 

-rw-r--r--. 1 user pg_user 1712804 Jul 13 22:27 postgresql-2021-07-13_000000.log
-rw-r--r--. 1 user pg_user       0 Jul 14 00:00 postgresql-2021-07-14_000000.log

$ ls -lt postgresql-*.log | head -n 1 
-rw-r--r--. 1 user pg_user       0 Jul 14 00:00 postgresql-2021-07-14_000000.log

now, I want to grep “string” in postgresql-2021-07-14_000000.log

How i can combine both in one command to get output with echo on screen. like below but someting is i am missing.

for example:

$ls -lt postgresql-*.log | head -n 1 | grep "system is ready" 

Thanks.





release – Testing a software automatically against more than 50 windows builds using VMware scripting?

Lets say you have a software (installer), and you want to automatically test it against more than 50-60 builds of windows (from windows xp to 10), and make sure it installs successfully, and do some stress testing and make sure there is no process crash or bugcheck (BSOD) happening.

The best thing that came to my mind was using VMware scripting and writing a script that uploads the latest build (installer) to the VMs (obviously on a very high end server), install it, does some stress testing stuff, collects some logs and reports backs the results.

Is this the best way to approach this? How do big companies deal with this? Is there any open source project for this?

scripting – Batch process to delete files

I’m a complete noob at this kinda thing but generally pretty competent with computers so I hope someone can humour me and point me in the right direction.

I’ve recovered a load of data off a dead hard drive using EaseUS data recovery and it’s produced many copies of each file and folder, usually about 8 or 9 copies. In the case of the folders, all but one of the copies are empty. In the case of the files, all but one of them are corrupt. The duplicated files are named in ascending numbers appended to the end of the filename after an underscore. I’ve sorted the folders using a tool which deletes any empty folders, but the files are proving more tricky as their file sizes are generally the same as that of the uncorrupted file.

What I want to do is delete all but the last file in the series of duplicates (as this is the one that isn’t corrupt in all the cases I’ve checked), and then rename that file to the original with no underscores / numbers. This would need to be done as an automated batch process as there’s many many files to get through.

I appreciate this is a very niche and specific problem but I really need to sort this data into a usable format. I know this will probably require me writing some of my own code but if anyone has any ideas I’d really appreciate it.

ubuntu – if statement – simple bash scripting

I am working on a simple script that checks if there is .vimrc file and what is configured in that file:

    if ( "$(ls -a /root | grep .vimrc)" = ".vimrc" ); then
    echo "The .vimrc file exists"

    if ( "$(cat /root/.vimrc | grep "colorscheme desert")" = "colorscheme desert" ); then
    echo "colorscheme is already added in the .vimrc file"
    else
    echo "The file does not have colorscheme added"
    fi

The first if statement is working fine, I get .vimrc = .vimrc thus echo "The .vimrc file exists"

But, the second if statement just prints “The file does not have colorscheme added”, but if I check that file, it has that part added:

cat /root/.vimrc | grep "colorscheme desert"

colorscheme desert

Thank you in advance!

Simple scripting language (interpreter) in C

Boolean type

If you really want / need a boolean type, use <stdbool.h>. If you don’t, just use an int as much prior code enshrines.
But for the love of all, don’t define your own TRUE. Remember that only null and zero are falsey, all else being truthy. Defining a constant invites comparison against it, which violates this convention imbued into the language itself.
Defining FALSE while also worrying at least won’t lead to compiling code which is logically wrong. But that is all it has going for itself.

Boolean logic

If you have a boolean value to return, return it directly, don’t branch on it and return literals. Unless you are paid by LoC, it’s a pointless obfuscation.

Unless you have a good reason, use boolean operators for boolean logic. Avoiding the short-circuiting is rarely important enough to use bitwise operators instead. And if you use it, remember it doesn’t short-curcuit.

Dead code

Eliminate dead code to avoid confusing or irritating readers.
After break, return, and the like, additional code in the block without intervening jump-label (in general, goto is to be avoided, as you do) or case-label is unreachable.

Initialize on declaration

Unless you want to run under strict C89, (just about) all compilers support mixing declarations and statements, and if you ask for any optimization, the result is generally the same.
Enhancing readability and just about eliminating the chance for a whole class of bugs for no cost is a bargain, right?

See “Is declaration of variables expensive?”.

Help finding bugs

If base to itoa() is out-of-range, that is a programming-error which should be found and fixed in development, never to be seen in release-mode. assert() is designed for that.

If you really want, you might kill the program in release too, trading efficiency for defense in depth, but don’t ever silently swallow the error. Doing so makes debugging a nightmare.

Be const-correct

Doing so enables the compiler to help you find bugs, and makes functions more self-documenting.

Use restrict where appropriate

Doing so allows the compiler to assume memory is not aliased, eliminating potential dependencies and increasing efficiency, in addition to making functions more self-documenting.

Line-length

Long lines are hard to read, especially if they lead to wrapping by the editor or horizontal scrolling.
172 is really a bit much, below 80 would be best.

String-literals are easily wrapped, as consecutive ones are merged by the compiler.

What is a string

A string is a sequence of non-nul elements including and terminated by the first nul element. str@@-functions are expected to handle strings, though there are infamous exceptions.

Single source of truth

The outer dimension of an array is sized according to the initializer if omitted. And you can get it back using (sizeof *array / sizeof array).

Unless there is a good reason to do otherwise, instead of an array of arrays of fixed length for storing string-literals, consider an array of pointers. It might be more efficient, and eliminates the forced fixed length.

switch and single label

An if-statements seems much more appropriate, as it is simpler.

Appropriate types

size_t is the designated type for object-sizes and array-indices. Unless you have a compelling reason, use it.

String length

Don’t blindly assume strings are a minimum number of characters long. Empty strings exist, and out-of-bounds access is generally Undefined Behavior.

Additional comments for specific code

  1. Why should itoa() only supply the minus-sign if the base is 10?
    That doesn’t seem to make any sense.

  2. strr() is just bad.

    • It looks more like a mem-function, as it doesn’t treat the input as a string, nor outputs a string.
    • r for reverse is very cryptic. Just spell it out.
    • Copying elements x to y both exclusive is a very surprising contract. If you don’t use a single length-parameter as expected (the caller can adjust the source-pointer appropriately), I would expect the first to be inclusive.
  3. equstr() is also an acquired taste.

    • It is not quite a string-function as it doesn’t terminate the target.
    • It really threw me for a loop that it modifies one argument, instead of comparing them, with the semantics of !strcmp(a, b).
  4. clear_str()

    • nulling out all of a strings elements is rarely needed. Generally, just truncating it by setting the first element to zero is sufficient.
  5. is_string()

    Are you sure anything starting with and ending with " is a string according to your grammar?

    • What about embedded "?
    • What about the length one string """?
    • What about escape-sequences?
  6. is_sint()

    • Ramp up the warning level. Your compiler should warn about the path without return-statement.
    • Are you sure only the first character should be tested?
    • Are you sure a signed integer must not be negative?

    When fixing that, don’t screw up the empty string.

  7. lang_arg_lens()

    • Where is the dynamic memory allocated freed again?
    • MAX_STRING_LENGTH seems wasteful.

Very simple scripting language (interpreter) in C

I made very simple scripting language (interpreter) in C and now, I want to make my code better.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE (1 == 1)
#define FALSE (1 == 0)

#define FUNCTION_COUNT 2
#define MAX_FUNCTION_NAME_LENGTH 247
#define MAX_FUNCTION_ARG_LENGTH 127

#define MAX_STRING_LENGTH 2048

char *strr(char *str, char *output, int x, int y)
{
    int i;
    for (i = x + 1; i < y; i++)
    {
        output(i - (x + 1)) = str(i);
    }
    return output;
}

int is_string(char *str)
{
    if (str(0) == '"' & str(strlen(str) - 1) == '"')
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

int is_sint(char *str)
{
    int i = 0;
    while (str(i))
    {
        if (str(i) >= '0' & str(i) <= '9')
        {
            return TRUE;
        }
        else
        {
            return FALSE;
            break;
        }
    }
}

void lang_prints(char *s)
{
    char output(MAX_STRING_LENGTH) = "";
    if (is_string(s))
    {
        strr(s, output, 0, strlen(s) - 1);
        printf("%sn", output);
    }
}

void lang_printi(int i)
{
    printf("%dn", i);
}

int main()
{
    int i;
    char function_names(FUNCTION_COUNT)(MAX_FUNCTION_NAME_LENGTH) = {
        "prints",
        "printi"
    };
    char input(MAX_STRING_LENGTH);
    char output(MAX_STRING_LENGTH);
    char function_name(MAX_FUNCTION_NAME_LENGTH) = "";
    char function_arg(MAX_FUNCTION_ARG_LENGTH) = "";
    printf("> ");
    scanf("%s", input);
    for (i = 0; i < FUNCTION_COUNT; i++)
    {
        strr(input, function_name, -1, strlen(function_names(i)));
        strr(input, function_arg, strlen(function_names(i)), strlen(input) - 1);
        if (strcmp(function_name, function_names(i)) == FALSE)
        {
            if (input(strlen(function_name)) == '(' & input(strlen(function_name) + strlen(function_arg) + 1) == ')')
            {
                switch (i)
                {
                    case 0:
                        lang_prints(function_arg);
                        break;
                    case 1:
                        if (is_sint(function_arg))
                        {
                            lang_printi(atoi(function_arg));
                        }
                        break;
                }
            }
        }
    }
}

(Use prints to print string and printi to print integer)

bash scripting – Is it normal for shell scripts to contain binary data?

I ask because I’ve been looking into Miniconda (https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh), and the installation shell script has TONS of binary data starting at line 577.
I ended up checking the shell script because Google Chrome flagged it as potential harmful. I know, it’s just Google Chrome, and any shell script may harm your computer, however the binary data starting at line 577 kinda scared me a little… if it’s not normal for installation shell scripts to contain a binary payload like that, then there’s no way I’m installing it.
But if it’s normal, then I’ll feel much better about installing Miniconda.

Top 10 Scripting Languages You Should Learn in 2021 | Forum Promotion

Scripting languages are used to write different scripts that contain several commands and are interpreted one by one at the runtime instead of compiling before running. Generally, Scripts are associated with web development and give instructions to the server, web browser, or standalone application to make dynamic web applications.

Keep in mind, all scripting languages are programming languages, but not all programming languages are scripting languages, so you need enough technical knowledge to use the appropriate one. Most popular coding languages such as PHP, JavaScript, Bash, Perl, Python, Ruby, and many others are scripting languages.

1. JavaScript

JavaScript is Object Oriented Programming (OOP) language in which objects are reused as prototypes, and it has first-class functions (treated as variables). It is known as the best implementation of ECMA-262 Standard of scripting languages and uses the same syntax as ECMAScript; therefore, it doesn’t have a standalone specification.

It is not limited to frontend web development; it can be used in runtime and non-browser environments. Moreover, shells of some NoSQL and relational database management systems such as MongoDB and aforementioned use it as a scripting language.

Full Article Source: https://www.temok.com/blog/top-10-scripting-languages/

Top 10 Scripting Languages You Should Learn in 2021

Scripting languages are used to write different scripts that contain several commands and are interpreted one by one at the runtime instead of compiling before running. Generally, Scripts are associated with web development and give instructions to the server, web browser, or standalone application to make dynamic web applications.

Keep in mind, all scripting languages are programming languages, but not all programming languages are scripting languages, so you need enough technical knowledge to use the appropriate one. Most popular coding languages such as PHP, JavaScript, Bash, Perl, Python, Ruby, and many others are scripting languages.

1. JavaScript

JavaScript is Object Oriented Programming (OOP) language in which objects are reused as prototypes, and it has first-class functions (treated as variables). It is known as the best implementation of ECMA-262 Standard of scripting languages and uses the same syntax as ECMAScript; therefore, it doesn’t have a standalone specification.

It is not limited to frontend web development; it can be used in runtime and non-browser environments. Moreover, shells of some NoSQL and relational database management systems such as MongoDB and aforementioned use it as a scripting language.

Full Article Source: https://www.temok.com/blog/top-10-scripting-languages/