linuxshell腳本wak語句中awk'BEGIN{FS="'"'"'";RS="\n"}{for(i=2;
FS表示操作的時候以什么為分割符RS表示記錄分割符(Record Separator)即RS表示的是awk操作最小單位的邊界,而FS是這個最小單位中分割的符號例如:101,John Doe:102,Jason Smith:103,Raj Reddy如果不指定RS,指定FS為“,”,那么awk將101作為第一列,剩下的全部為第二列,如果指定RS=":",那么awk就會將內容作為三“行”,第一列包括101,102和103,第二列包括John Doe,Jason Smith和Raj Reddy。
shell腳本 ,如果用 for in 語句,讀取一行內容,急用,請高手回答
#!/bin/sh
#設置IFS環境變量,這個變量是用來定義分隔符類型的。默認的分隔符為空格、換行符、tab。
#在你的需求中,只需要設置IFS變量的內容為換行符。
#先存儲IFS默認值
IFS_old=$IFS
IFS=$'\n'
for line in $(ls -l --full-time)
do
echo $line
done
#將IFS變量還原為默認值
IFS=$IFS_old
shell腳本怎么寫for循環
for I in list; do
statement
done
I 是變量
list是一個表格 如你可以使用一串用括號括起來的數,
也可以使用 命令替換 `seq 1 15` 這個命令忘記了,, 有可能是 `seq 15`
[1..15]
表示1-15的數,,
statement 即要執行的語句
for I in [1..10]; do
echo $I
done
這段for循環的含義就是顯示從1~10的所有數字
linux shell腳本用到循環、控制語句
#!/bin/sh# Name: useraddmuti# Descripton: To add users to your system. Users can be list in a file.# To exec this command your ID must be 0.# Author: PopZslam@*#-----------------------------------chkUID(){ getUID(){ id|sed -e 's/(.*$//' -e 's/^uid=//' } if [ "`getUID`" -ne 0 ] then echo -e "\tYou are not root!" exit 0 fi }chkUIDusagePRT(){ echo ${USAGE:='USAGE:' `basename $0` '-f namelistfile'} }chkFILE(){ if [ ! -z "`awk 'NF!=2{print NF;exit;}' $1`" ] && [ "`awk 'NF!=2{print NF;exit;}' $1`" -ne 2 ] ; then echo -e "The file's format is not right!" exit 0 fi }userCHK(){ for USER in `awk '{print $1;}' $1` do if grep -wq $USER /etc/passwd ; then echo -e "The user($USER) has been added!" exit 1 fi if echo $USER|grep -wq "^[0-9].*" ; then echo -e "The user($USER)'s name is wrong format!" exit 1 fi done }setOPT(){ echo -e "Now Let's set some options or you can use default settings." setGRPNAME(){ while : do echo -e "Would you like to add a new group to add these users to it?" echo -e "Enter YES to create a new group otherwise you must verify the group." printf "Your Answer: " read grpopt case $grpopt in yes) printf "Please enter the group's name: " read grpoptnew if cat /etc/group|sed 's/:.*//'|grep -wq $grpoptnew ; then echo "The group's name($grpoptnew) exist." exit else grpname=$grpoptnew echo -e "All these users will be added to group($grpname)。
" echo -e "Adding group 。" if cp /etc/group /etc/group.$$ >; /dev/null 2>;&1 ; then if groupadd $grpname ; then echo -e "The group($grpname) is added!" rm -f /etc/group.$$ break 1 else echo -e "There's something wrong when adding the group($grpname)." echo -e " *** Please recovered the group file. *** " echo -e "You can cp /etc/group.$$ to /etc/group to recover." fi else echo "Error! Please check the program or your disk space." exit 0 fi fi ;; *) : ;; esac done } setGRPNAME }addUSER(){ if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then for user in `sed 's/ .*//' $1` do pass=`awk '{ $1~/$name/ {print $2;exit} } name=$user' $1` if [ -z "$pass" ] ; then echo -e "The passwd is used by default sun123." pass=sun123 fi if [ ${#pass} -lt 6 ] ; then echo -e "The user($user)'s password is too short!" echo -e "Use default password: sun123." pass=sun123 fi if useradd $user ; then echo -e "The user($user) is added." if echo $pass|passwd $user --stdin >; /dev/null 2>;&1 ; then echo -e "The user($user)'s password is setted!" else echo -e "The user($user)'s password is NOT set!" fi else echo -e "The user($user) is NOT add." fi done rm -f /etc/passwd.$$ /etc/shadow.$$ else echo -e "There something wrong when backup the passwd and shadow file." fi }if [ $# -ne 2 ] ; then usagePRT exit 0ficase "$1" in -f) if [ -f "$2" ] ; then echo -e "Reading usernamelist file""("$2")" "。
" chkFILE $2 userCHK $2 setOPT addUSER $2 else echo -e "There's no usernamelist file!" fi ;; *) usagePRT exit 0 ;;esac。
求大神解答 shell腳本中的循環語句 謝謝
假如你的txt中,每一行有一個名字,假設文件名是*
i=0
while read line
if [ $i -eq 10 ]
then
break
fi
//處理內容,每一行中獲取的內容,也就是名字已經賦值在line當中,可以直接通過他使用
//例子
if [ "$line" = "example" ]
then
echo "Hello example"
break
else
(( i++ ))
fi
done
shell腳本for循環,N到1遞減寫法
為什么識別不了?
n=10
for ((i=n;i>=1;i--))
do
echo $i
done在Bash下測試是可以的。
你還可以使用for。in。語句或while語句。
for i in {10..1} #這里不能用變量
do
echo $i
done
n=10
while [ $n -ge 1 ] #或 while ((n>=1))
do
echo $n
let n-=1
done
轉載請注明出處華閱文章網 » shell腳本for語句