note of SHELL (一)

#----------------------------------------
#note:copy suffix with character "r" or "g"
#   to "../test" directory

#!/bin/sh
for file in [rg]* ;do
  mv $file ../test
  done

#----------------------------------------

 

 

 

#--------------------------------------------------


echo "choose the city "
echo "1) WH"
echo "2) BJ"
echo "3) DN"

read input
case $input in
1) echo "you choose WuHan";;
2) echo "you choose BeiJin";;
3) echo "you choose DaNian";;
*) echo "input something number please!!!";;
esac

 

 

 

 

#--------------------------------------------
#name:check_file.sh
#note:check the arguments and file
#!/bin/sh

if [ $# -lt 1  ];then
  echo "usage:`basename $0` filename"
  echo
fi

if [ ! -f $2  ];then
  echo "file is not exist"
fi
  

 

 

 

#---------------------------------
#name:check_root.sh
#note:check whether the user is root

ROOT_ID=0
if [ $UID -ne $ROOT_ID ] ; then
  echo -e  "you should change to root/n"
  exit 1
  fi

if [ -z $1 ] ;then
  echo -e "miss arguments/n"
  exit 1
  fi

 

 

#-----------------------------------------------------------
#name:continue.sh
#note:print 1 through 20 except 4 and 14

for ((a=1;a<21;a++));do
 if [ $a -eq 4 ] || [ $a -eq 14 ];then
   continue
 fi
 echo "$a"
 done
 

#-----------------------------------------------------------

 

 

 

#------------------------------------------------
#name:cut.sh
#note:print usrs in this computer
#!/bin/sh
FILE="/etc/passwd"
for user in $(cut -d: -f1 $FILE);do
  echo "$user"
  done

 

 

#-----------------------------------------------------
#name:nested_loop.sh
#note:for loop function nested for function
#     output 25 lines
#!/bin/sh
LIMIT=6
for((a=1;a<LIMIT;a++));do
  echo "the out loop is $a"
for((b=1;b<LIMIT;b++));do
  echo "the inter loop is $b"
  done

done
exit 0
 

#----------------------------------------------------

 

 

 

#------------------------------------------------
#name:find_exec.sh
#note:exec function used in find,look care "/;" at
# the end of the sentense
#!/bin/sh
#find /home/peter/shell  -name "test.txt" -exec rm {} /;
find .  -name "test.txt" -exec rm {} /;

 

 

#-------------------------------------
#name:for.sh
#using  for function like C Language in shell

#!/bin/sh
LIMIT=10
for ((a=1;a<=LIMIT ; a++));do
  echo "$a"
  done
#-------------------------------------

 

 

 

 #!/bin/bash
  #This script print ip and network
 file="/etc/sysconfig/network-scripts/ifcfg-eth0"
   if [ -f $file ] ;then
                IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
       MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
                echo "$IP/$MASK"
               exit 1
               fi
          

 

#------------------------------------------------------------
#name:find_dir.sh
#list dir in $1 and print the result to out.txt

#!/bin/sh
ARG=1
OUTPUT="out.txt"
if [ $# -ne "$ARG" ]
#I can not understand here,I think the option should be "-eq"

  then
  DIR=`pwd`
  else
    DIR="$1"
fi

echo "----------directory list-------------" >>$OUTPUT
echo " this shell script are following"
for file in "$(find $DIR -type d)" ;do
  echo "$file"|tee -a $OUTPUT
  done
  

 

 

#-------------------------------------------------------------
#name:nested_continue.sh
#note:"continue 2" means skipped the second for loop function

for group in 1 2 3 4 5;do
  echo "--group:$group--"
  for ((inter=1;inter<=10;inter++));do
    if [ $inter -gt 5 ];then
      continue 2
    fi
    echo "inter:$inter"
    done
    done 
exit 0


 

#-------------------------------------------------------------
#name:nested_continue.sh
#note:"continue 2" means skipped the second for loop function

for group in 1 2 3 4 5;do
  echo "--group:$group--"
  for ((inter=1;inter<=10;inter++));do
    if [ $inter -gt 5 ];then
      continue 2
    fi
    echo "inter:$inter"
    done
    done 
exit 0


 

#read.sh
#read -t 10  delay 10s during input
#!/bin/sh
echo "input in 10s "
read -t 10 a
if [ -z $a ] ;then
echo "input again"
else
echo "input finish"
fi


 

#--------------------------------------------------------
#name:select.sh
#note:usage select function
#!/bin/sh

echo "choose your favorite fruit"

select fruit banana orange apple ;do
echo
echo "your choice is  $fruit"
break
done
exit 0

 

 

#-----------------------------------
#name:shift.sh
#note:print the argument

while [ ! -e $1 ];do
  echo -e "$1/n "
 # echo
  shift
  done

 

 

 

#--------------------------------------------
#name:source.sh
#note: source function can insert other file
#      into this file

#!/bin/sh
source  test.sh
echo $a

 

 

 

#-----------------------------------
#name:while.sh
#note:input something and exit by type "end"

echo "input your sentense,type "end" to exit"
while [ "$var" != "end"  ];do   
#be careful ! $var must be quoted
  read var
  done
  exit 0
  

 

 

#------------------------------------------------
#name:test_character.sh
#note:pick out the character you input

echo "please hit a character and type enter"
read a
case $a in
[a-z] ) echo "lower character";;
[A-Z] ) echo "upper character";;
[0-9] ) echo "digital";;
* ) echo "other symbol";;
esac

 

 

 

#----------------------------------------------------
#!/bin/sh
#name:test_equal.sh
#note:test two things whether are equal
clear
if [ $# -lt 2 ];then
  echo "usage:$0 ARG1 ARG2"
  exit 1
  fi
test()
{
  case $1 in
  $2) echo "they are the same things ";;
  * )  echo "they are different";;
  esac
}

test $1 $2
exit 0

 

 

 

 

#name:test_file_exist.sh
#note:if the file in $FILE is not exist,then print warning,else print the filename
#     and file size


#!/bin/sh
FILE="random.sh
read.sh
/bin/hi
"
for a in $FILE;do
  if [ ! -e $a ];then
    echo "warning :$a is  no exist!!!"
    continue
       fi
    echo
    echo
    ls -l $a|awk  '{print "file_name:"$9 "file_size:"$5}'
    
    done

 

 

 

#----------------------------------------------
#name:tr_d.sh
#note:delete colon from $1,and output a new file
#!/bin/sh
newname=$1.u
if [ -z $1 ] ;then
  echo -e "usage:`basename $0` filename_to_convert/n"
  exit 0
  fi
  tr -d ":"< $1 >$newname

 

 

### Shell Scripting or Shell Commands Shell scripting is a fundamental aspect of Unix-like operating systems, including Linux and macOS. It involves writing scripts that automate repetitive tasks, manage system resources, and interact with other programs. The shell itself serves as both an interactive command interpreter and a scripting language[^1]. #### Special Meanings of Parentheses in Shell Scripts Parentheses `(` and `)` have specific meanings in shell scripting, particularly in bash. They are used for creating subshells, grouping commands, and defining arrays. For example: - **Subshells**: `(command1; command2)` creates a subshell where the commands inside the parentheses are executed in isolation from the main shell environment. - **Command Grouping**: `{ command1; command2; }` groups commands together but does not create a subshell. Note the semicolon after each command and the braces. - **Array Definitions**: `array=(value1 value2 value3)` defines an array named `array` with three elements. When using parentheses literally in commands like `tcpdump`, they must be escaped with a backslash (`\`) so the shell interprets them as literal characters rather than part of its syntax[^1]. ```bash tcpdump \( host 192.168.1.1 \) ``` #### External Programs in Shell Scripts Many useful commands within shell scripts are external Unix utilities rather than built-in shell commands. Some common external utilities include: - `tr`: Translating or squeezing characters. - `grep`: Searching files for lines that match patterns. - `expr`: Evaluating expressions. - `cut`: Removing sections from each line of files. Built-in commands such as `echo`, `test`, and `which` are often directly integrated into the shell, providing faster execution without invoking an external process[^2]. #### Identifying Directories and Executables The `ls` command can help identify directories and executable programs. By using the `-F` flag, `ls` appends a `/` to directory names and a `*` to executable program names. This feature aids in quickly distinguishing between different types of files when navigating or scripting in the shell[^3]. ```bash ls -F /path/to/directory ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值