Linux 命令行入门

来源:互联网 发布:店淘精品数据采集神器 编辑:程序博客网 时间:2024/06/16 13:23

  • linux 命令行入门
    • 打印输出
    • 定义变量
    • 控制结构
    • 常见命令
    • 函数
    • 帮助命令
    • 推荐

linux 命令行入门

参考learn X in Y minutes

1.打印输出

echo Hello world! # => Hello world!
echo ‘This is the first line’; echo ‘This is the second line’
# => This is the first line
# => This is the second line
# 在一行的多个echo命令以分号分隔,在每一行分别输出echo的命令

2.定义变量

Variable=”Some string” # 定义变量
Variable = “Some string”
# error,Bash会将Variable解析成一个命令;所以变量赋值的时候,等号两边不要有空格
echo $Variable # => Some string
echo “$Variable” # => Some string
echo ‘$Variable’ # => $Variable
# 注意定义赋值变量的时候不需要加$,使用的时候需要;单双引号也是有区别的,双引号会展开变量,单引号则不会

echo ${Variable} # => Some string
echo ${Variable/Some/A} # A string
# 更多字符串截取方法请参考Shell脚本8种字符串截取方法总结

Length=7
echo ${Variable:0:Length} # => Some st
# 输出固定长度的子串

echo ${Foo:-“DefaultValue”} # => DefaultValue
# 如果Foo的值没定义或在为空,则将DefaultValue赋值给它

echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z

echo “Last program’s return value: $\?” # => $\?表示上一个程序的返回值
echo “Script’s PID: $$” # => $$表示脚本的PID
echo “Number of arguments passed to script: $#” # => $#表示传递给脚本参数的个数
echo “All arguments passed to script: $@” # => $@表示传递给脚本的参数
echo “Script’s arguments separated into diffrent variables: $1 $2…” # => 分别用12表示脚本的第几个参数
# 几个常用的内置变量

echo “I’m in $(pwd)” # => 执行pwd命令,并将结果插入到字符串中
echo “I’m in $PWD” # => $PWD是内置变量,效果等于$(pwd)

echo “What’s your name?”
read Name # => 从标准输入中读入值赋给变量Name
ehco Hello, $Name!

3.控制结构

if [ $Name != $USER ]
then
    echo “Your name isn’t your username”
else
    echo “Your name is your username”
fi
# 输入man test,获取更多比较信息

# 更多控制结构参考shell编程控制结构

echo $((10 + 5)) # => 15

4.常见命令

ls # 列出当前文件夹下所有文件和子文件夹
# man ls;获得更多详细信息
# 不懂就问man,哈哈哈。。。

cat file.txt # 将file的内容打印到终端
Contents = $(cat file.txt) # 将file的内容赋值给Contents

cp # 复制命令
# man cp ^_^

mv # 移动命令
# man mv

cd ~ # 切换到家目录
cd .. # 切换到上一级目录
cd - # 切换到上一次cd之前的目录

cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print(“#stdout”, file=sys.stdout)
print(“#stderr”, file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF
# 将标准输入的内容写如到hello.py中去,遇到EOF停止

python hello.py < “input.in” # 将input.in作为参数传递给脚本hello.py
python hello.py > “output.out” # 将hello.py的输出重定向到output.out
python hello.py 2> “error.err” # 将hello.py的标准错误输出重定向到error.err
python hello.py > “output-and-error.log” 2>&1 # 将标准输出和标准错误输出重定向到output-and-error.log
python hello.py > /dev/null 2>&1 # 不输出标准输出和标准错误输出
python hello.py >> “output.out” 2>>”error.err” # 如果output.out和error.err存在;>>表示追加,不覆盖里面的内容

echo “There are $(ls | wc -l) items here”
echo “There are ls | wc -l items here”
# ()()是可以嵌套的

for Variable in {1..3}
do
    echo “$Variable”
done
# => 1
# => 2
# => 3

for ((a=1; a <= 3; a++))
do
    echo $a
done

for Variable in file1 file2
do
    cat “$Variable”
done

for Output in $(ls)
do
    cat “$Output”
done

# 几种常见的for循环的形式

while [ true ]
do
    echo “loop body here…”
    break
done
# while循环要注意设置跳出循环的条件

5.函数

function foo ()
{
    echo “Arguments work just like script arguments: $@”
    echo “And: $1 $2..”
    return 0
}

foo arg1 arg2 # 调用该函数
# => Arguments work just like script arguments: arg1 and arg2
# => And: arg1 arg2…
# => This is a function

tail -n 10 file.txt # 输入文件的后10行
head -n 10 file.txt # 输出文件的前10行

sort file.txt # 排序

uniq -d file.txt # 输出重复的行

cut -d ‘,’ -f 1 file.txt # 以,分隔file,取出第一列

sed -i ‘s/okay/great/g’ file.txt # 将文件中okay替换成great

grep “^foo.*bar$” file.txt # 匹配以foo开头以bar结尾的行

6.帮助命令

man # 精确查找帮助
apropos # 模糊查找帮助
help # 模糊查找shell内置命令帮助文档

7.推荐

每天一个linux命令

原创粉丝点击