2.1-基础知识-shell - study - 414

54 阅读1分钟
#!/bin/bash
#when i test my code -ex-xe-e-x these drop are not useful and inspire a error  
#第一行表示锁定解释器,#!表示不忽略此条
# remember to chmod 755 yourscript.sh,no need of ./
# then you tap ./yourscript.sh to run this .sh file
# finally you will see your answer

#echo "My First Script!"
#echo表示向终端输出内容。

read -p "Please Enter Your Name:" NAME
#read giveNAME (variable) some context.

echo "$(hostname)"
echo "NAME = "st""

if [ $NAME = "yay" ]
#need $ to declare your variable. 
then
        echo "my host:)"
else
        echo "you are a bad guy:("
fi

case "$NAME" in
        [yY][Aa][yY] | [yY])
        echo ":)"
        ;;
        *)
        echo ":("
        ;;
esac

COLORS="red green blue"
#Captical alpha differs system function.
for COLOR in $COLORS
#NO need[]
do
        echo "color is:${COLOR}"
                                                                                                                                              
###         顶端

do
        echo "color is:${COLOR}"
done
#when you are giving some context to this variable
# do not use this plain input.

FILES=$(ls *txt)
#* is equal to all.
NEW="new"

for FILE in $FILES
do
        echo "renaming $FILE to new-$FILE"
#Tell your enemy to join this war.
        mv $FILE $NEW-$FILE
#mv is meaning changing name?txt the docu?
done

# all loop function has the same struct.
# for conditon(no need of []
#do
#done
#continue is to break current loop
# break is to break whole loop setence/

#NAME can be used through all the .sh file.
# from 0---while 0 is to this script.sh.1- defined variable.

# same to c++,while main is completr correctly /return 0.
# to check wheather the function is conducted right.

#LINE = 1
#while read CURRENT_LINE
#do 
#       echo"$LINE:$CURRENT_LINE"
#       ((LINE++))
#done </etc/passwd
# read function i dont konw
# CURRRENT_LINE,LINE is random name?

##  43,0-1        74%
 # end this etc/passwd ,why?

HOST="google.com"
ping -c 1 $HOST
RETURN_CODE=$?
# Result is saved in this $? variable.amazing.
if [ "RETURN_CODE" -eq "0" ]
then
        echo "$HOST is reachable."
else
        echo "$HOST is unreachabe."
fi