QA Shell Script

·

4 min read

Cron job to be scheduled on every last day of the month

59 23 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && /path/to/your/command

Explanation of the fields:

  • 59 23: The cron job will run at 23:59 (almost midnight).

  • 28-31: The job is scheduled for the 28th, 29th, 30th, and 31st day of the month.

  • *: The job will run every month.

  • The condition [ "$(date +\%d -d tomorrow)" == "01" ] checks if tomorrow is the 1st day of the month. If true, it means today is the last day of the month.

  • /path/to/your/command: Replace this with the actual command or script you want to run.

Make sure to adjust the path to your actual command or script. Additionally, note that the syntax may vary slightly depending on the specific cron implementation on your system. The example above assumes a standard cron syntax used in many Unix-like systems, including Linux.

You can add this entry to your crontab using the crontab -e command and then pasting the line at the end of the file.


Cron job & Contab

  1. Cron Jobs:

    • A cron job is a task or command that is scheduled to run at specified intervals.
  2. Crontab:

    • The crontab (cron table) is the configuration file that contains the list of cron jobs for a user.

Alternative for "if else" conditional statement

Ternary Operator

condition ? expression_if_true : expression_if_false

Changing the Shell

The chsh command allows you to change your default login shell. Here's how you can use it:

  1. Open a terminal.

  2. Type the following command and press Enter:

     chsh
    

    This will prompt you to enter your password for verification.

  3. After entering your password, you will be prompted to enter the new shell. Type the path to the new shell (e.g., /bin/bash, /bin/zsh, etc.) and press Enter.

    For example, to change your shell to Bash, you would enter /bin/bash.

  4. The chsh command will confirm the change, and the next time you log in, the new shell will be used.

Please note the following considerations:

  • You need the necessary privileges to change your shell. If you don't have the required permissions, you may need to contact your system administrator.

  • Make sure the shell you are switching to is installed on your system. You can use the which command to check if a shell is available. For example:

      which bash
    
  • Changing your default shell doesn't affect the shell you are currently using in an open terminal session. It only applies to new login sessions.

  • It's important to be aware of the syntax and characteristics of the new shell you are switching to, as different shells may have different features and syntax.

  • If you are unsure about the available shells on your system, you can check the contents of the /etc/shells file, which typically lists the valid login shells.


XARGS

It takes input from standard input (or other specified sources), converts it into arguments, and passes those arguments to a specified command

Here are some common use cases and options for xargs:

Using with Find:

find /path/to/search -name "*.txt" | xargs rm

This will find all files with the extension ".txt" and pass them as arguments to the rm command.

Replacing String in File Names:

ls | xargs -I {} mv {} {}.bak

This will add a ".bak" extension to all files in the current directory.


Difference between bash profile and /etc/profile

Both the ~/.bash_profile and /etc/profile files play roles in configuring the Bash shell environment, but they serve different purposes and are used in different contexts:

  1. ~/.bash_profile:

    • The ~/.bash_profile file is a user-specific configuration file for Bash. It is typically located in a user's home directory (~) and is executed when a user logs in. If ~/.bash_profile doesn't exist, Bash may look for ~/.bash_login or ~/.profile as alternatives.

    • Users can customize their individual environment settings in this file. It often includes user-specific environment variable settings, custom aliases, and other configurations specific to a particular user's preferences.

  2. /etc/profile:

    • The /etc/profile file is a system-wide configuration file for Bash. It is located in the /etc directory, making it applicable to all users on the system. The commands in this file are executed when any user logs in.

    • The purpose of /etc/profile is to set system-wide environment variables and define global configurations that should apply to all users. It's typically used for settings that are common across the entire system.

In summary:

  • ~/.bash_profile is a user-specific file for individual customization and is executed when a user logs in.

  • /etc/profile is a system-wide file for configuring the environment for all users and is executed when any user logs in.


set -v

In Bash scripting, the set -v command is used to enable verbose mode, which causes the shell to print each command to the standard error output (stderr) before executing it. This can be useful for understanding how a script is executing and for debugging purposes.

It's important to note that set -v is equivalent to set -o verbose, and you can use set +v (or set +o verbose) to turn off verbose mode.