Bash

From My Wiki
Jump to: navigation, search

Hello World

#!/bin/bash
echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
 horse | dog | cat) echo -n "four";;
 man | kangaroo ) echo -n "two";;
 *) echo -n "an unknown number of";;
esac
echo " legs."

select fname in *;
do
	echo you picked $fname \($REPLY\)
	break;
done

echo a{d,c,b}e

echo {0,1}{0,1}{0,1}
#!/bin/bash
echo "Input 5 integers [0-100]"
read a
read b
read c
read d
read e
echo "$((($a+$b+$c+$d+$e)/5))"
for i in {0780..1063}; do echo `printf "%04d" $i`; done

1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063

/usr/bin/4chan

#!/bin/bash
ID=$(echo $1 | grep -o 'res/[^"]*' | egrep "[0-9]{1,}" -o );
if [ -d $ID ]; then
echo $ID folder exists
cd $ID
curl $1 | grep -o 'File[^"]*<a href=\"//images.4chan.org/[^"]*' | grep -o '//images.4chan.org/[^"]*' > imgurl2.x
for line in $(cat imgurl2.x); do echo "http:"$line >> imgurlnew.x; done;
comm -13 imgurl.x imgurlnew.x > diff.x
wget -i diff.x
cat diff.x >> imgurl.x
rm diff.x
rm imgurlnew.x
rm imgurl2.x
cd ..
else
echo $ID folder does not exist
mkdir $ID
cd $ID
curl $1 | grep -o 'File[^"]*<a href=\"//images.4chan.org/[^"]*' | grep -o '//images.4chan.org/[^"]*' > imgurl2.x
for line in $(cat imgurl2.x); do echo "http:"$line >> imgurl.x; done;
wget -i imgurl.x
rm imgurl2.x
cd ..
#!/bin/bash
URL=$(echo $1)
if [[ "$URL" !=  *'#'* ]]; then
        ID=$(echo $URL | grep -o 'res/[^"]*' | egrep "[0-9]{1,}" -o );
else
        ID=$(echo $URL | grep -o 'res/[^"]*\#' | egrep "[0-9]{1,}" -o );
fi
if [ -d $ID ]; then
        echo $ID folder exists
        cd $ID
else
        echo $ID folder does not exist
        mkdir $ID
        cd $ID
fi
curl $URL | grep -o 'File[^"]*<a href=\"//images.4chan.org/[^"]*' | grep -o '//images.4chan.org/[^"]*' > imgurlpre.x
for line in $(cat imgurlpre.x); do echo "http:"$line >> imgurl.x; done;
rm imgurlpre.x;
for line in $(cat imgurl.x); do wget -nc $line; done;
rm imgurl.x

cd ..
#!/bin/bash
URL=$(echo $1)
if [[ "$URL" !=  *'#'* ]]; then
        ID=$(echo $URL | grep -o 'res/[^"]*' | egrep "[0-9]{1,}" -o );
else
        ID=$(echo $URL | grep -o 'res/[^"]*\#' | egrep "[0-9]{1,}" -o );
fi
if [ -d $ID ]; then
        echo $ID folder exists
        cd $ID
else
        echo $ID folder does not exist
        mkdir $ID
        cd $ID
fi
curl $URL | grep -o 'File[^"]*<a href=\"//images.4chan.org/[^"]*' | grep -o '//images.4chan.org/[^"]*' > imgurl.x
for line in $(cat imgurl.x); do wget -nc "http:"$line ; done;
rm imgurl.x
cd ..

.bashrc / .profile


# extractions
extract () {
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xvjf $1    ;;
      *.tar.gz)    tar xvzf $1    ;;
      *.tar.xz)    tar xvJf $1    ;;
      *.bz2)       bunzip2 $1     ;;
      *.rar)       unrar x $1     ;;
      *.gz)        gunzip $1      ;;
      *.tar)       tar xvf $1     ;;
      *.tbz2)      tar xvjf $1    ;;
      *.tgz)       tar xvzf $1    ;;
      *.zip)       unzip $1       ;;
      *.Z)         uncompress $1  ;;
      *.7z)        7z x $1        ;;
      *.xz)        unxz $1        ;;
      *.exe)       cabextract $1  ;;
      *)           echo "\`$1': unrecognized file compression" ;;
    esac
  else
    echo "\`$1' is not a valid file"
  fi
}


How does ln (symbolic links) work?

ln /original/file /new/link

ln -s /path/to/file /path/to/symlink # soft link (not a hard link)

root@localhost:~/test# mkdir -p a/b/c/d/e/f/
root@localhost:~/test# mkdir -p 1/2/3/4/5/6/
root@localhost:~/test# ln -s a/b/c/ 1/2/3/4
root@localhost:~/test# cd 1
root@localhost:~/test/1# ls
2
root@localhost:~/test/1# cd 2
root@localhost:~/test/1/2# ls
3
root@localhost:~/test/1/2# cd 3
root@localhost:~/test/1/2/3# ls
4
root@localhost:~/test/1/2/3# cd 4
root@localhost:~/test/1/2/3/4# ls
5  c

rm 1/2/3/4/c # rm symlink don't use trailing slash (file not folder)


root@localhost:~/test# ln -s a/b/c/ 1/2/3/4
root@localhost:~/test# ls 1/2/3/4
5  c

date formatting

date -u "+%FT%T"
date -u "+%F %T"
date -u "+%F %T" | sed -r 's/[ -:]*//g'

grep

grep \sclass\s file
\s - space

fix colours

Blue (color for highlights is annoying). To fix in bash

cat ~/.dircolors
DIR 01;36

Then "source" the .bashrc file

cd && . .bashrc
"source": When a file is sourced (by typing either source filename or . filename at the command line), the lines of code in the file
 are executed as if they were printed at the command line. This is particularly useful with complex prompts, to allow them to be stored
 in files and called up by sourcing the file they are in.

In examples, you will find that I often include #!/bin/bash at the beginning of files including functions. This is not necessary if you
 are sourcing a file, just as it isn't necessary to chmod +x a file that is going to be sourced. I do this because it makes Vim
 (my editor of choice, no flames please - you use what you like) think I'm editing a shell script and turn on colour syntax highlighting.

http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x237.html

In vim do

:set background=dark

pick random

boards=(a b c d e f g gif h hr k m o p r s t u v vg vr w wg i ic r9k s4s cm hm lgbt y 3 adv an asp biz cgl ck co diy fa fit gd hc int jp lit mlp mu n out po pol sci soc sp tg toy trv tv vp wsg x)
num_boards=${#boards[*]}
this_board="${boards[$((RANDOM%num_boards))]}"
sudo python main.py $this_board > "log_"$this_board 2> "log_"$this_board"_err" &

loop

#!/bin/bash

for i in a b c d e f g
    do echo "a"$i
done