Bash

From wiki
Revision as of 07:34, 26 July 2014 by Administrator (talk | contribs)
Jump to navigation Jump to search

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

cat ~/.dircolors
DIR 01;36

In vim do

:set background=dark
#!/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