Skip to content

Instantly share code, notes, and snippets.

@rzymek
Created March 15, 2018 11:11
Show Gist options
  • Save rzymek/628ee9e62a2aaea2a2b6ec4318ad1cdd to your computer and use it in GitHub Desktop.
Save rzymek/628ee9e62a2aaea2a2b6ec4318ad1cdd to your computer and use it in GitHub Desktop.
bash scripting workshop notes

Onelines

wget -r -l1 www.lingarogroup.com/about-us

cd www.lingarogroup.com  # use TAB completion

parameter expantion

Done by the shell, before it gets to the command:

echo c*

Same as:

echo careers cloud-solutions computer-vision consumer_reasearch

Let's view all images together: find

find -name '*.png'  

Note quoting of '*' here. Compare

echo c*
echo 'c*'

Loop over results

for f in a b c;do  echo "val: $f"; done

So:

for f in $(find -name '*.png');do echo f=$f ;done

Note: f is a variable.

for f in $(find -name '*.png');do 
    echo $f|cut -c3-|tr / _ ;
done

See help:

cut -h
cut --help
man cut

Assign the new name to variable:

for f in $(find -name '*.png');do 
    name=$(echo $f|cut -c3-|tr / _ );
    echo "$f -> $name"
done

Note " quoting. It allows for variable expantion. Needed here because > is redirect. Needs to be passed to echo, not interpreted by shell.

for f in $(find -name '*.png');do 
    name=$(echo $f|cut -c3-|tr / _ );
    ln -s "$f" "$name"
done
ls
pcmanfm  # file browser
rm *.png

Turn it into a script

code img.sh

img.sh:

#!/bin/bash
for f in $(find -name '*.png');do   
    name=$(echo $f|cut -c3-|tr / _ ); 
    ln -s "$f" "$name";
done

Set executable flag:

chmod +x img.sh

Expand the script

#!/bin/bash
set -e #2
mkdir -p out  #1, #3: -p
for f in $(find -name '*.png');do
    name=$(echo $f|cut -c3-|tr / _ )
    ln -s "../$f" "out/$name";  #1
done

xargs

find www.lingarogroup.com/ -name '*.png'|grep -v 18/02 | xargs rm -v

3 types of quotes

X=1
touch 1 2 "3 4"
touch X1=$X "X2=$X" 'X3=$X'
touch 'date: `date`'
touch "date: `date`"

regex

[abc]
^begin and end$
.
a*
cat|dog
[a-z][0-9]

debug

bash -x 

#variables

X=1
xterm

export Y=1

date=`date +%Y%m%d`
echo app-$date.log
X=20
echo "width $Xpx"
echo "width ${X}px"

test [ ]

-z ""
-f file
-d dir
! -z "$x"

if [ ]
if [[ ]]
if ((  ))

Complete script

#!/bin/bash
set -eu
cd `dirname $0`
out=${1:-}
if [ -z "$out" ];then
    echo "Usage: $0 [output-dir]"
    exit 1
fi
mkdir -p ${out}
for f in $(find -name '*.png');do 
    name=$out/$(echo $f | cut -c3-| tr / -)
    if [ -e "$name" ];then
        echo "Already exists $(basename $f)"
    else
        ln -s "../$f" "$name"
    fi
done 

Links

http://redsymbol.net/articles/unofficial-bash-strict-mode/
http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming
https://kvz.io/blog/2013/11/21/bash-best-practices/
https://www.regular-expressions.info/quickstart.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment