Difference between revisions of "Shell"

From Wiki at Neela Nurseries
Jump to: navigation, search
m (minor correction)
m (^ Git Diff-tree Helper Script: - amend script with additional git oneline formatting, support for count of commits to handle.)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Shell Scripting
 
Shell Scripting
  
 +
== [[#top|^]] OVERVIEW ==
 +
 +
This local page for Bash shell scripts and notes on shell scripting.
 +
 +
== [[#top|^]] Bash Built-in Variables ==
 +
Some links to useful articles discussing `bash` built-in variables:
 +
*  https://avpres.net/Bash/builtin_shell_variables
 +
*  https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
 +
 +
Bash's built-in variables with short names, as listed in Kernigan and Ritchie's "Unix Programming" book and at the first linked article above:
 +
 +
<pre>
 +
$0
 +
    The first element passed to the shell is the command name.
 +
$n
 +
    The nth argument passed on the command line. If n ≥ 10, then the syntax must be ${n}.
 +
$*
 +
    All the arguments on the command line. The values are separated by the first character in the shell variable IFS: (${1} … ${n}). See also: the IFS entry in Other Shell Variables.
 +
$@
 +
    All the arguments on the command line. The values are individually quoted: ("${1}" … "${n}").
 +
$#
 +
    The number of command-line arguments.
 +
$?
 +
    The exit value of the last executed command.
 +
$_
 +
    The last argument of the previous command.
 +
$!
 +
    The process ID of the most recent background process.
 +
</pre>
 +
 +
<!-- odne komentar -->
 +
== [[#top|^]] Rename Badly Named Files ==
  
 
How to produce a usable filename which contains <ESC> characters, and to rename that badly named file:
 
How to produce a usable filename which contains <ESC> characters, and to rename that badly named file:
  
 +
  $ ls -i
 +
  9704871 ''$'\033\033'  9703029  CMakeLists.txt  10899590  dts          9708845  samples
 
   $ ls -q `find . -inum 9704871`
 
   $ ls -q `find . -inum 9704871`
 
   './'$'\033\033'
 
   './'$'\033\033'
 
   $ mv './'$'\033\033' betterfilename
 
   $ mv './'$'\033\033' betterfilename
 +
 +
<!-- odne komentar -->
 +
<span id="nn_anchor__shell__helper_script_git_diff-tree"></span>
 +
== [[#top|^]] Git Diff-tree Helper Script ==
 +
 +
A helper script for calling `git diff-tree`, which can be used to determine at which project commit one or more files have changed:
 +
 +
<pre>
 +
#!/bin/bash
 +
 +
CMD_GIT=/usr/bin/git
 +
 +
$CMD_GIT log --oneline | head | cut -d " " -f 1
 +
 +
echo "2024-01-31 git diff-tree helper script in progress . . ."
 +
echo "got $# arguments, arg one single quoted is '$1'"
 +
 +
hashes=`$CMD_GIT log --oneline | head -n ${1} | cut -d " " -f 1`
 +
echo "In $PWD found git commit hashes:"
 +
echo " " $hashes
 +
 +
#i=1
 +
#for hash in $hashes; do echo "("$i")" $hash; (( i++ )); done
 +
 +
#echo "Files changed between commit pairs youngest pairings to oldest:
 +
#for hash in $hashes; do echo "Files changed in git commit:"; git diff-tree -r $hash; echo; done
 +
for hash in $hashes
 +
    do echo "Files changed in git commit:"
 +
    git log -1 --oneline $hash
 +
    git diff-tree -r $hash
 +
    echo
 +
done
 +
 +
echo "done"
 +
 +
exit 0
 +
<!-- odne komentar -->

Latest revision as of 07:08, 1 March 2024

Shell Scripting

^ OVERVIEW

This local page for Bash shell scripts and notes on shell scripting.

^ Bash Built-in Variables

Some links to useful articles discussing `bash` built-in variables:

Bash's built-in variables with short names, as listed in Kernigan and Ritchie's "Unix Programming" book and at the first linked article above:

$0
    The first element passed to the shell is the command name.
$n
    The nth argument passed on the command line. If n ≥ 10, then the syntax must be ${n}.
$*
    All the arguments on the command line. The values are separated by the first character in the shell variable IFS: (${1} … ${n}). See also: the IFS entry in Other Shell Variables.
$@
    All the arguments on the command line. The values are individually quoted: ("${1}" … "${n}").
$#
    The number of command-line arguments.
$?
    The exit value of the last executed command.
$_
    The last argument of the previous command.
$!
    The process ID of the most recent background process.

^ Rename Badly Named Files

How to produce a usable filename which contains <ESC> characters, and to rename that badly named file:

 $ ls -i
 9704871 $'\033\033'   9703029  CMakeLists.txt  10899590  dts          9708845  samples
 $ ls -q `find . -inum 9704871`
 './'$'\033\033'
 $ mv './'$'\033\033' betterfilename

^ Git Diff-tree Helper Script

A helper script for calling `git diff-tree`, which can be used to determine at which project commit one or more files have changed:

#!/bin/bash

CMD_GIT=/usr/bin/git

$CMD_GIT log --oneline | head | cut -d " " -f 1

echo "2024-01-31 git diff-tree helper script in progress . . ."
echo "got $# arguments, arg one single quoted is '$1'"

hashes=`$CMD_GIT log --oneline | head -n ${1} | cut -d " " -f 1`
echo "In $PWD found git commit hashes:"
echo " " $hashes

#i=1
#for hash in $hashes; do echo "("$i")" $hash; (( i++ )); done

#echo "Files changed between commit pairs youngest pairings to oldest:
#for hash in $hashes; do echo "Files changed in git commit:"; git diff-tree -r $hash; echo; done
for hash in $hashes
    do echo "Files changed in git commit:"
    git log -1 --oneline $hash
    git diff-tree -r $hash
    echo
done

echo "done"

exit 0