Linux 基本命令

时间:2019-09-16
本文章向大家介绍Linux 基本命令,主要包括Linux 基本命令使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
-o   or    或者     -a   and   与
a>0 &  ||    b>10
-z  zero  -n  number  
[ -n $d ] 等同于 [ $d ]
变量不需要预先定义,直接用
byte  -b , char  -c  ,directory  -d , file  -f,  exist  -e

给shell脚本传递参数/给函数传递参数
其他编程语言传递参数的方式  :add(12,10)  
shell编程语言: add  12  10
其他编程语言获取返回值的方式  :ret=add(12,10)  
shell编程语言获取返回值的方式  :add 12  10   ;    echo $?
1、
echo "first name:"
read f
echo "last name:"
read l
echo "$f $l"
2、
echo "first num:"
read f
echo "last num:"
read l
echo $[$f * $l]

shell  脚本里可以执行shell命令
3.
touch hello$(date +%M%H%S)
4、  如果当前目录存在子目录dir1,如果在就将dir1改名为dir2,
       如果不存在,就新建一个子目录dir1.
if  [ -e  dir1 ];then  mv dir1  dir2;else mkdir dir1;fi

5. 输入3个数,打印最大的数

  read -p "input a num:"  n1
  read -p "input a num:"  n2
  read -p "input a num:"  n3
   max=$n1
   if [ $n2 -gt $max ] 
  then
	max=$n2
   fi
  if [ $n3 -gt $max ] 
  then
	max=$n3
   fi
   echo $max

6. 让用户输入一个单词,如果输入no,就退出,如果输入其他的,就继续提示输入
  while true
  do
  read -p "input a word" w
  if [ $w == "no" ]
  then
    break
  else
     read -p "input a word" w
  fi
  done

作业:
1.请建立一支 script ,当你执行该 script 的时候,该 script 可以显示: 1. 你目前的身份 (用 whoami ) 2. 你目前所在的目录 (用 pwd)
2.让用户输入一个数字,程序可以由 1+2+3... 一直累加到用户输入的数字为止
3.撰写一支程序,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若不存在,则建立一个文件,使用 touch 来建立,建立完成后离开; 3.) 如果存在的话,判断该名称是否为文件,若为文件则将之删除后建立一个目录,文件名为 logical ,之后离开; 4.) 如果存在的话,而且该名称为目录,则移除此目录
4.我们知道 /etc/passwd 里面以 : 来分隔,第一栏为账号名称。请写一只程序,可以将 /etc/passwd 的第一栏取出,而且每一栏都以一行字符串『The 1 account is "root" 』来显示,那个 1 表示行数。
cat /etc/passwd | cut -d":" -f1

原文地址:https://www.cnblogs.com/richlovesherry/p/11526496.html