Monday, May 31, 2021

 

Different Format Specifiers available in C


Format specifiers define the type of data to be printed on standard output. You need to use format specifiers whether you're printing formatted output with printf() or accepting input with scanf().

Below are some of the format specifiers that are available in ANSI C

SPECIFIER

USED FOR

%c

a single character

%s

a string

%hi

short (signed)

%hu

short (unsigned)

%Lf

long double

%n

prints nothing

%d

a decimal integer (assumes base 10)

%i

a decimal integer (detects the base automatically)

%o

an octal (base 8) integer

%x

a hexadecimal (base 16) integer

%p

an address (or pointer)

%f

a floating point number for floats

%u

int unsigned decimal

%e

a floating point number in scientific notation

%E

a floating point number in scientific notation

%%

the % symbol










Friday, May 28, 2021

Shift command in Linux with examples

 Shift command in Linux with examples

                                                                                                    Source:  https://www.geeksforgeeks.org 

Shift is a built-in command in bash which after getting executed, shifts/move the command line arguments to one position left. The first argument is lost after using shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command line arguments which are not needed after parsing them.

Syntax:

shift n

Here, n is the number of positions by which you want to shift command-line arguments to the left if you do not specify, the default value of n is assumed to be 1 i.e shift works the same as shift 1.

Example: Let’s create a shell script file named as sampleshift.sh as follows. The total number of command-line arguments is represented by $#. Use the following command to create the desired shell script file

vi sampleshift.sh

Now paste the following code:

#!/bin/bash

# total number of command-line arguments
echo "Total arguments passed are: $#"

# $* is used to show the command line arguments
echo "The arguments are: $*"

echo "The First Argument is: $1"
shift 2

echo "The First Argument After Shift 2 is: $1"
shift

echo "The First Argument After Shift is: $1"

Now to save the file press ESC and then type “:x” without quotes and hit Enter. Now to execute the file, use the following command on Linux terminal

sh sampleshift.sh

But here we have to pass command-line arguments so we can use the following command

sh sampleshift.sh G1 G2 G3 G4

Here, we are passing 4 command-line arguments named G1, G2, G3, and G4. Below is the screenshot of the output of using shift command:

Shift-Command-in-Linux-with-Examples



Functions in Shell Scripts


#!/bin/sh
# A simple script with a function...

add_a_user()
{
  USER=$1
  PASSWORD=$2
  shift; shift;
  # Having shifted twice, the rest is now comments ...
  COMMENTS=$@
  echo "Adding user $USER ..."
  echo useradd -c "$COMMENTS" $USER
  echo passwd $USER $PASSWORD
  echo "Added user $USER ($COMMENTS) with pass $PASSWORD"
}

###
# Main body of script starts here
###
echo "Start of script..."
add_a_user bob letmein Bob Holness the presenter
add_a_user fred badpassword Fred Durst the singer
add_a_user bilko worsepassword Sgt. Bilko the role model
echo "End of script..."


Below is the output after execution of above script (test1.sh)








Sunday, May 9, 2021