(2.1) Linux Shell 스 크 립 트 의 자동화 프로 그래 밍 셸 변수
26468 단어 새 형 리 눅 스 개인 요리
1. 변수의 정의
: = , ,
:$ ${
}
:echo $ set( : )
:unset
: shell
# ip1=1.2.34.1
# echo $ip1
#
:
1:export back_dir2=/home/backip
2:export back_dir1
:$ ${
}
:echo $ env eg:env|grep back_dir2
:unset
: shell shell
eg: ( shell ),
public.sh :
ip10=1.1.1.1
dir_path=/etc/wangji
test1.sh :
. public.sh ## , ,. shell
echo "ip10 is: $ip10"
echo "dir_path is: $dir_path"
。
, public.sh :export ip10 dir_path,
eg: , /new/bin ?
vim /etc/profile
:
PATH=$PATH:/new/bin
export PATH
,
source /etc/profile ##source shell
echo $PATH
=
$ :
#!/usr/bin/bash
ip=10.18.42.1
ping -c1 $ip &>/dev/null && echo "$ip is up" || echo "$ip is down"
-----------------------------------------------------------------------------------
if then ( , type -a if, type -a if ), ; 。
:if ping -c1 $ip &>/dev/null; then
ping -c1 $ip &>/dev/null
#!/usr/bin/bash
ip=10.18.42.1
if ping -c1 $ip &>/dev/null; then ## :if ping -c1 $ip &>/dev/null
echo "$ip is up" ##then
else
echo "$ip is down"
fi
----------------------------------------------------------------------------------
#!/usr/bin/bash
ip=10.18.42.1
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
else
echo "$ip is down"
fi
----------------------------------------------------------------------------------
#!/usr/bin/bash
read -p "Please input ip: " ip ##
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
else
echo "$ip is down"
fi
----------------------------------------------------------------------------------
$1
#!/usr/bin/bash
ping -c1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
$1 $2 $3 ${
4}
$0
$*
$@
$#
$$ PID
$! PID
$? ,0
eg:
ip.txt , :
1.1.2.3
1.2.3.4
1.2.3.5
#!/usr/bin/bash
# ,
if [ $# -eq 0 ];then
echo "uasge: 'basename $0' file" #basename ,eg:basename /home/wangji/cc, cc
fi
##
if [ ! -f $1 ];then
echo "erro file!"
fi
for ip in 'cat $1'
do
ping -c1 $ip &>dev/null
if [ $? -eq 0 ];then
echo "$ip is up"
else
echo "$ip is down"
if
done
: ./ping01.sh /etc/ip.text
2. 변수의 할당
=
eg:
ip1=1.1.1.2
school="Wangji hello"
today=`data+%F;echo $today` ##`` , ( , ), today=(data+%F)
today=$(data+%F)
##
read
read
read -p " : "
read -t 5 -p " : " ## -t 5 : 5s
read -n 2
eg:
#!/usr/bin/bash
read -p " [eg: zhangsan 20]:" name age
echo " :$name, :$age"
""
'' , ,
eg:
name="wang ji"
boy="$name is good";echo $boy
wang ji is good
boy='$name is good';echo $boy
$name is good
`` : , $(), shell
eg:
touch `date+%F`_file.txt touch (date+%F)_file.txt
disk1=“df -h|grep '/$'|awk '{print $4}'”
echo $disk1 , ,
:
disk1=`df -h|grep '/$'|awk '{print $4}'`
echo $disk1
disk1=(df -h|grep '/$'|awk '{print $4}')
echo $disk1