Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Friday, November 11, 2016

Untrack files from git using git bash

This will start ignoring the changes to the file:
git update-index --assume-unchanged path/to/file 

When you want to start keeping track again: 
git update-index --no-assume-unchanged path/to/file 

Now list files that are ignored: 
git ls-files -v 

And with a search options: 
git ls-files -v | grep "file_name_part"

Sunday, August 25, 2013

HowTo Format Date For Display or Use In a Shell Script

How do I format date to display on screen on for my shell scripts as per my requirements on Linux or Unix like operating systemsYou need to use the standard date command to format date or time. You can use the same command with the shell script.
 The syntax is
  1. date +FORMAT 
  2. date +"%FORMAT"
  3. date +"%FORMAT%FORMAT" 
  4. date +"%FORMAT-%FORMAT"

Task: Display date in mm-dd-yy format

Open a terminal and type the following date command:
$ date +"%m-%d-%y"Sample output:
02-27-07
To turn on 4 digit year display:
$ date +"%m-%d-%Y"Just display date as mm/dd/yy format:
$ date +"%D"

Task: Display time only

Type the following command:
$ date +"%T"Outputs:
19:55:04
To display locale's 12-hour clock time, enter:
$ date +"%r"Outputs:
07:56:05 PM
To display time in HH:MM format, type:
$ date +"%H-%M"Sample outputs:
00-50

How do I save time/date format to the shell variable?

$ NOW=$(date +"%m-%d-%Y"To display a variable use echo / printf command:
$ echo $NOW

A sample shell script

#!/bin/bash
NOW=$(date +"%m-%d-%Y")
FILE="backup.$NOW.tar.gz"
echo "Backing up data to /nas42/backup.$NOW.tar.gz file, please wait..."
# rest of script
# tar xcvf /nas42/backup.$NOW.tar.gz /home/ /etc/ /var
 

A complete list of FORMAT control characters supported by the date command

FORMAT controls the output. It can be the combination of any one of the following:
%FORMAT StringDescription
%%a literal %
%alocale's abbreviated weekday name (e.g., Sun)
%Alocale's full weekday name (e.g., Sunday)
%blocale's abbreviated month name (e.g., Jan)
%Blocale's full month name (e.g., January)
%clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%Ccentury; like %Y, except omit last two digits (e.g., 21)
%dday of month (e.g, 01)
%Ddate; same as %m/%d/%y
%eday of month, space padded; same as %_d
%Ffull date; same as %Y-%m-%d
%glast two digits of year of ISO week number (see %G)
%Gyear of ISO week number (see %V); normally useful only with %V
%hsame as %b
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%mmonth (01..12)
%Mminute (00..59)
%na newline
%Nnanoseconds (000000000..999999999)
%plocale's equivalent of either AM or PM; blank if not known
%Plike %p, but lower case
%rlocale's 12-hour clock time (e.g., 11:11:04 PM)
%R24-hour hour and minute; same as %H:%M
%sseconds since 1970-01-01 00:00:00 UTC
%Ssecond (00..60)
%ta tab
%Ttime; same as %H:%M:%S
%uday of week (1..7); 1 is Monday
%Uweek number of year, with Sunday as first day of week (00..53)
%VISO week number, with Monday as first day of week (01..53)
%wday of week (0..6); 0 is Sunday
%Wweek number of year, with Monday as first day of week (00..53)
%xlocale's date representation (e.g., 12/31/99)
%Xlocale's time representation (e.g., 23:13:48)
%ylast two digits of year (00..99)
%Yyear
%z+hhmm numeric timezone (e.g., -0400)
%:z+hh:mm numeric timezone (e.g., -04:00)
%::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
%Zalphabetic time zone abbreviation (e.g., EDT)
SEE ALSO:

Call and store response from a url using bash script

#!/bin/bash

## generate random string of length 18

function randpass
{
        echo `</dev/urandom tr -dc A-Za-z0-9 | head -c18`
}


## get current time stamp

timestamp=$(date  +%s);
sessionId="$(randpass).$(randpass).$timestamp";
privateKey='test';
 

## get substring of sessionId from 0 to 5 characters.
sessionIdPart=${sessionId:0:5};
systemKeyPart=${privateKey:0:4};


## reverse string and store in a variable

word="$(echo "$sessionIdPart" | rev)$(echo "$systemKeyPart" | rev)";
word="$word$sessionId$privateKey";
 

## make a string md5
hash=`echo -n "$word" | md5sum | awk '{print $1}'`;
hash=${hash:0:10};
breakRest="securedSessionKey=$sessionId&securedHash=$hash";
 

saveFile=$(date +"%Y-%m-%d %T")
## saveFile would be '2013-08-25 19:23:28'

## calling a url using 'curl' and store response to some file.
curl -s -L -o "$saveFile.html" "http://domain.com/getStudent?$breakRest" &
## "&" sign at the end means run curl in background.

exit 1;

Wednesday, June 19, 2013

bash split string into array

IFS=', ' read -a array <<< "$string" /* array is the variable name */
To access an individual element:
echo "${array[0]}"
To iterate over the elements:
for element in "${array[@]}"
do
    echo "$element"
done
To get both the index and the value:
for index in "${!array[@]}"
do
    echo "$index ${array[index]}"
done
The last example is useful because Bash arrays are sparse. In other words, you can delete an element or add an element and then the indices are not contiguous.
unset "array[1]"
array[42]=Earth

How to set a BASH variable equal to the output from a command?

In addition to the backticks, you can use $(), which I find easier to read, and allows for nesting.
OUTPUT=$(ls -1)
echo $OUTPUT
 
 
You can use eval to execute a string:
eval $illcommando
 

Sunday, February 24, 2013

Tuesday, December 11, 2012

Check if a program exists from a bash script

$ command -v foo >/dev/null 2>&1 || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}
OR 
$ type foo >/dev/null 2>&1 || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}
OR 
$ hash foo 2>/dev/null || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}