顯示具有 bash script 標籤的文章。 顯示所有文章
顯示具有 bash script 標籤的文章。 顯示所有文章

2016年8月4日 星期四

[Script] How to local in shell script

#!/bin/bash
# ex62.sh: Global and local variables inside a function.

func ()
{
  local loc_var=23       # Declared as local variable.
  echo                   # Uses the 'local' builtin.
  echo "\"loc_var\" in function = $loc_var"
  global_var=999         # Not declared as local.
                         # Therefore, defaults to global. 
  echo "\"global_var\" in function = $global_var"
}  

func

# Now, to see if local variable "loc_var" exists outside the function.

echo
echo "\"loc_var\" outside function = $loc_var"
                                      # $loc_var outside function = 
                                      # No, $loc_var not visible globally.
echo "\"global_var\" outside function = $global_var"
                                      # $global_var outside function = 999
                                      # $global_var is visible globally.
echo                      

exit 0
#  In contrast to C, a Bash variable declared inside a function
#+ is local ONLY if declared as such.

Reference:

2015年10月2日 星期五

[bash] kill process id by search process name

2015年8月17日 星期一

2015年7月17日 星期五

2015年6月29日 星期一

2015年4月26日 星期日

2015年2月12日 星期四

2014年11月4日 星期二

[Bash] Create cscope.out and tags by using script

Usage :

Type bash ctags_cscope.sh /home/haha/20141103-11-54/project

This command will create four file like "cscope.in.out  cscope.po.out cscope.out tags" under /home/haha/20141103-11-54/project.

Code :

#!/bin/bash
DATE=`date +%m%d`
PWD=`pwd`
Directory=$1
function ctags_cscope()
{
#  [ Ctrl + \ + s ] : 搜尋游標上的 function 哪邊參考到
#  [ Ctrl + \ + c ] : 搜尋游標上的 function 哪邊呼叫到
#  [ Ctrl + \ + g ] : 搜尋游標上的 function 是在哪邊定義的
#  
#  [ Ctrl + \ + t ] : 跳回下一個位置
#  [ Ctrl + \ + o ] : 跳回上一個位置
    cd $Directory

    CSCOPE_FILE_TEMP=cscope.out
    if [ -n $Directory ]; then
  echo "Source code directory: " $1
        echo "Create file map database : " $CSCOPE_FILE_TEMP
  find $1 -name "*.h" -or -name "*.c" -or -name "*.cpp" -or -name "*.cc" > $CSCOPE_FILE_TEMP
  cscope -bkq -i $CSCOPE_FILE_TEMP
        ctags -R
    else
        echo "Please type path of project"
    fi  }
# Main Code
ctags_cscope

Manual :
cscope -bkqR

2013年11月8日 星期五

[.bashrc] Understand what is the content of .bashrc mean


[.bashrc] Understand what is the content of .bashrc mean



\u     @   \h         :   \w  \$



freeman@freeman-laptop:~/folder$







Big w just show the current folder

\u     @   \h         :   \W  \$







\[\033[00m\]   Clean the color

\[\033[01;32m\]  Green

${debian_chroot:+($debian_chroot)}\[\033[00m\][\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\] \W\[\033[00m\]\$]

[freeman@freeman-laptop: ~$]





Reference:



2013年10月8日 星期二

[bash][For loop][Function] Write a script to do a routine job


[bash][For loop][Function] Write a script to do a routine job



#!/bin/bash





eoo()

{

    xdotool mousemove  643  427

    xdotool click 2

    sleep 1



    xdotool key Home

    sleep 1

    xdotool key Home

    sleep 1



    for i in $(seq 1 1 $1)

    do

       xdotool key Down

    done





    sleep 0.2

}  



foo()

{



    xdotool mousemove  816  426

    xdotool click 2

    sleep 1



    xdotool type $1

    xdotool key Tab

    sleep 0.1

    xdotool key 'Return'



    sleep 2



}





##### Press Number  #####

echo "Please press Number first"

sleep 2



##### add routers   #####





eoo 3

foo '111.6.1.254'



##### add time-servers   #####



eoo 4

foo '111.2.1.2'









##### add domain-name-servers  #####



eoo 6

foo '111.2.1.213'







##### add dhcp-lease-time   #####



eoo 49

foo '1w'







##### add tftp-server  #####



eoo 60

foo '111.2.1.148'







##### add time-servers   #####



eoo 98

foo '(primary-dhcp-server 1 111.2.1.213)'





 


2013年8月15日 星期四

[tcpdump] How to use tcpdump to capture packet



1. Install tcpdump

sudo apt-get install tcpdump

2. Using the command to catch the packet.

& put the command into the background

-s capture size 10000 bytes

sudo tcpdump host 10.15.109.41 and port 5060 -s 10000 -w results/1.pcap &

Remeber the last the pid of  command to pid1.
pid1=$!


ex :

happy@happy-laptop:~/test1/good$ sudo tcpdump host 1.1.1.1 and port 5060 -s 10000 -w test.pcap &

[1] 13291

happy@happy-laptop:~/test1/good$ pid1=$!

happy@happy-laptop:~/test1/good$ echo $pid1

13291

Reference:

2013年5月27日 星期一

2013年5月17日 星期五

[GitHub] How to upload the code to github

[GitHub] How to upload the code to github

First  :
Make sure the key have post github websit.
Step 1: Check for SSH keys
cd ~/.ssh

Step 2: Generate a new SSH key
ssh-keygen -t rsa -C "your_email@example.com"

Step 2-2: Then add your new key to the ssh-agent:
ssh-add id_rsa

Step 3: Add your SSH key to GitHub
sudo apt-get install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub

Step 4: Test everything out
ssh -T git@github.com

# Hi username! You've successfully authenticated, but GitHub does not provide shell access.

#####################################################################
Second  :
Start to upload the code to github.
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:freeman2004/test.git
git push -u origin master

Push an existing repository from the command line
git remote add origin git@github.com:freeman2004/test.git
git push -u origin master
#####################################################################

Q:
 ! [rejected]        master -> master (non-fast-forward)
A:
git pull -u origin master    <-- Pull the master branch from github

Reference :

[Arch Linux][USB][Update 2013-05-17][Auto Script] Install arch linux 2013 on the USB stick


[Arch Linux][USB][Update 2013-05-17][Auto Script] Install arch linux 2013 on the USB stick



Following Script Download




 



#!/bin/sh

#



set -x # Show the message of each command

#MODULE FUNCTIONS{{{



arch_chroot () { #{{{

arch-chroot $ARCH_ROOT /bin/bash -c "${1}"

}

#}}}



#}}}

# ARCH Root

ARCH_ROOT=/mnt



# file system

mkfs -t ext4  /dev/sda1



# mount file system

mount /dev/sda1 $ARCH_ROOT



# pacman base

pacstrap -i $ARCH_ROOT base grub-bios

sleep 2



# fstab

genfstab -U -p $ARCH_ROOT >> $ARCH_ROOT/etc/fstab

sleep 2



# Set timezone and hwclock

arch_chroot 'hwclock --systohc --utc'

sleep 2



# Set hostname

arch_chroot 'echo "freeman-arch" > /etc/hostname'

sleep 2



# DHCP start

arch_chroot 'systemctl enable dhcpcd.service'

sleep 2



# locale

echo "LANG=zh_TW.UTF-8" > /etc/locale.conf

sleep 2



# locale-gen

arch_chroot 'sed -i \

-e "/^#en_US ISO-8859-1/s/#//" \

-e "/^#en_US.UTF-8 UTF-8/s/#//" \

-e "/^#zh_TW.UTF-8 UTF-8/s/#//" \

-e "/^#zh_TW BIG5/s/#//" \

/etc/locale.gen'

arch_chroot 'locale-gen'

sleep 2



# users and passwd

arch_chroot 'useradd -m -G users,wheel -s /bin/bash freeman'

arch_chroot 'echo "freeman:freeman" > passwd.txt'

arch_chroot 'echo "root:root" >> passwd.txt'

arch_chroot 'chpasswd < passwd.txt'

arch_chroot 'rm passwd.txt'

sleep 2



# mkinitcpio

arch_chroot 'mkinitcpio -p linux'

sleep 2



# grub

arch_chroot 'grub-mkconfig -o /boot/grub/grub.cfg'

arch_chroot 'grub-install /dev/sda'

sleep 2



#rm $ARCH_ROOT/vbox_arch_chroot.sh

umount $ARCH_ROOT/

shutdown -r now



# Reference:







2013年2月17日 星期日

2013年2月4日 星期一

[Bash] Print out how many number of arguments and arguments


Bash Script :



args=("$@")

Number=("$#")

echo $# arguments passed



for i in $(seq 0 1 $Number)

do

   echo ${args[$i]}

done



Result :


Happy@Happy-laptop:~/Desktop$ bash test123.sh 1 2 3 4 5 6  7  8

8 arguments passed

1

2

3

4

5

6

7

8



Reference :

2013年1月21日 星期一

[Bash] How to pass string with space as parameter


Create A ABC.htm

<html>

<head>

<title>are you ready</title>



Create a Function Script

#!/bin/bash



Re()

{

    echo $1

    echo $PWD

    sed -n -e '/'"$1"'/p' $PWD/ABC.htm > output



    FILE='output'



    if [ -s $FILE ] ; then

        rm $FILE

    else

        echo $FileName' '$1 >> $PWD/Error.log

        rm $FILE

    fi

}



Create Execute Script

#!/bin/bash

PWD=`pwd`

source $PWD/happy.sh



var1='>are you ready<'

var2='<head>'







Re "$var1"

Re "$var2"





Reference :



2013年1月20日 星期日

[Bash] Put all function into one file


[Bash] Put all function into one file




  1. Create three bash file call A B C

    A :





    #!/bin/bash

    print1()

    {

        echo $1

    }



    print2()

    {

        echo $1

        echo $2

        echo $3

    }



    B:

    #!/bin/bash



    PWD=$(pwd)

    echo $PWD

    source $PWD/A.sh



    b=$1

    print1 $b



    C:

    #!/bin/bash



    PWD=$(pwd)

    echo $PWD

    source $PWD/A.sh





    print2 $1

    print2 $2

    print2 $3

     


  2. Excute B

    The Result

    bash B.sh 'ABCD' 'B' 'D'

    /home/freeman/Desktop/test

    ABCD

     


  3. Excute C

    The Result



    bash C.sh 'ABCD' 'B' 'D'

    /home/freeman/Desktop/test

    ABCD





    B





    D



 



Reference :



2013年1月16日 星期三

[Bash][function] Passing parameters to a bash function


#!/bin/sh
foo 1  # this will fail because foo has not been declared yet.

foo() {
    echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output :
happy@happy-laptop:~/Desktop/test2$ sh test3.sh
test3.sh: 3: foo: not found
Parameter #1 is 2

Reference :