Skip to content

Latest commit

 

History

History
361 lines (278 loc) · 7.97 KB

b1.linux.md

File metadata and controls

361 lines (278 loc) · 7.97 KB

Linux

rsync

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD。
-v, --verbose 详细模式输出。
-z, --compress 对备份的文件在传输时进行压缩处理。
nohup rsync -av --progress /Share/home/xugang /WORK/teaching>log.txt 2>&1 &

Tar选项:

  • c – 创建压缩文件
  • x – 解压文件
  • v – 显示进度.
  • f – 文件名.
  • t – 查看压缩文件内容.
  • j – 通过bzip2归档
  • z –通过gzip归档
  • r – 在压缩文件中追加文件或目录
  • W – 验证压缩文件

把目录/home/abc/code 打包为code.tar

tar -cvf code.tar /home/abc/code/

压缩为 tar.gz 格式的包

tar cvzf code.tar.gz /home/abc/code

压缩率更高的 tar.bz2 格式的包

tar cvfj code.tar.bz2 /home/abc/code/

解压 tar 包

tar -xvf code.tar -C /home/abc/code

解压 tar.gz 包

tar -xvf code.tar.gz

解压 tar.bz2 包

tar -xvf code.tar.bz2

grep

grep $'\t' file.txt

find

# find by name
find . -name pro\*
# grep by recursion
grep -r update /etc/acpi 

zip

zip -r foo.zip foo

paste

paste file testfile testfile1 #合并指定文件的内容 
##
$ cat file                  #file文件的内容  
xiongdan 200  
lihaihui 233  
lymlrl 231  
$ cat testfile              #testfile文件的内容  
liangyuanm  ss  
$ cat testfile1             #testfile1文件的内容  
huanggai 56  
zhixi 73 
#
xiongdan 200  
lihaihui 233  
lymlrl 231  
liangyuanm  ss  
huanggai 56  
zhixi 73  

join

combinate file by column.

Use tab to paste file.

join -t $"\t" file1 file2
#
join testfile_1 testfile_2 

#
$ cat testfile_1 #testfile_1文件中的内容  
Hello 95 #例如,本例中第一列为姓名,第二列为数额  
Linux 85  
test 30  
cmd@hdd-desktop:~$ cat testfile_2 #testfile_2文件中的内容  
Hello 2005 #例如,本例中第一列为姓名,第二列为年份  
Linux 2009  
test 2006 

#
$ join testfile_1 testfile_2 #连接testfile_1、testfile_2中的内容  
Hello 95 2005 #连接后显示的内容  
Linux 85 2009  
test 30 2006 

Replace whitespaces with tabs in linux

sed -i 's/ \+/\t/g' inputfile 
sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt
sed 's/ \+/\t/g' inputfile > outputfile

nohup

nohup command > myout.file 2>&1 &

ps

ps -aux|grep bowtie

auto password

# 1. Generate SSH key
ssh-keygen -t rsa -b 2048
# 2. Copy your keys to the target server:
ssh-copy-id user@server_ip # if port add: -p 2200

paste

paste -d $'\t' name.txt summary.txt > summary2.txt

Bash Function Declaration

The syntax for declaring a bash function is very simple. They may be declared in two different formats: The first format starts with the function name followed by parentheses. This is the preferred and more used format.

function_name () {
  commands
}

Single line version:
```sh
function_name () { commands; }

The second format starts with the function reserved word followed by the function name.

function function_name {
  commands
}

Single line version:

function function_name { commands; }

Few points to be noted:

  • The command list between curly braces {} is the body of the function. The curly braces that surround the function body must be separated from the body by spaces or newlines.
  • Defining a function doesn’t execute it. To invoke a bash function, simply use the function name. Commands between the curly braces are executed whenever the function is called in the shell script.
  • The function definition must be placed before any calls to the function.
  • When using single line “compacted” functions, a semicolon ; must follow the last command in the function.
  • You should always try to keep your function names descriptive.

To understand this better, take a look at the following example:

~/hello_world.sh

#!/bin/bash

hello_world () {
   echo 'hello, world'
}

hello_world

Let’s analyze the code line by line:

In line 3 we are defining the function by giving it a name, and opening the curly brace { that marks the start of the function’s body. Line 4 is the function body. The function body can multiple commands. Line 5, the closing curly bracket }, defines the end of the hello_world function. In line 7 we are executing the function. You can execute the function as many times as you need. If you run the script it will print hello, world.

Variables Scope

Global variables are variables that can be accessed from anywhere in the script regardless of the scope. In Bash all variables by default are defined as global, even if declared inside the function.

Local variables can be declared within the function body with the local keyword and can be used only inside that function. You can have local variables with the same name in different functions.

To better illustrate how variables scope works in Bash, let’s consider an example:

~/variables_scope.sh

#!/bin/bash

var1='A'
var2='B'

my_function () {
  local var1='C'
  var2='D'
  echo "Inside function: var1: $var1, var2: $var2"
}

echo "Before executing function: var1: $var1, var2: $var2"

my_function

echo "After executing function: var1: $var1, var2: $var2"

The script starts by defining two global variables var1 and var2. Then a function that sets a local variable var1 and modifies the global variable var2.

If you run the script, you should see the following output:

Before executing function: var1: A, var2: B
Inside function: var1: C, var2: D
After executing function: var1: A, var2: D

~/return_values.sh

#!/bin/bash

my_function () {
  echo "some result"
  return 55
}

my_function
echo $?

some result

55

To actually return an arbitrary value from a function we need to use other methods. The simplest option is to assign the result of the function to a global variable:

~/return_values.sh

#!/bin/bash

my_function () {
  func_result="some result"
}
my_function
echo $func_result
Copy
some result

Another, better option to return a value from a function is to send the value to stdout using echo or printf like shown below:

~/return_values.sh

#!/bin/bash

my_function () {
  local func_result="some result"
  echo "$func_result"
}

func_result="$(my_function)"
echo $func_result
some result

Instead of simply executing the function which will print the message to stdout, we are assigning to the func_result variable using the $() mechanism. Using this method the func_result variable holds the result of the function.

Passing Arguments to Bash Functions

To pass any number of arguments to the bash function simply put them right after the function’s name, separated by a space. It is a best practice to double quote the arguments to avoid misparsing of an argument with spaces in it.

The passed parameters are $1, $2, $3 … $n, corresponding to the position of the parameter after the function’s name.

The $0 variable is reserved for the function’s name.

The $# variable holds the number of positional parameters/arguments passed to the function.

The $* or $@ variable holds all positional parameters/arguments passed to the function.

Here is an example:

~/passing_arguments.sh

#!/bin/bash

greeting () {
  echo "Hello $1"
}

greeting "Joe"

Hello Joe

Conclusion

By now you should have a good understanding of how to write bash functions. You may also want to read about how to use a Bash function to create a memorable shortcut command for a longer command. If you have any question or feedback, feel free to leave a comment.

for 1 to 25

for i in {1..25};do echo $i;done;

find

find . -name pro\*

function

#!/bin/sh

# Define your function here
Hello () {
   echo "Hello World $1 $2"
   return 10
}

# Invoke your function
Hello Zara Ali

# Capture value returnd by last command
ret=$?

echo "Return value is $ret"

$./test.sh Hello World Zara Ali Return value is 10