當前位置:首頁 » 操作系統 » linuxshellifs

linuxshellifs

發布時間: 2022-05-07 12:39:02

❶ 請教大神shell的for in語句中怎麼使用多個變數

1、遍歷按空格分隔的字元串:

#!/bin/bash

servers="abc123 das 222"

for server in$servers

do

echo$server

done

2、遍歷字元串列表、用空格分隔:

#!/bin/bash

for server in a b c

do

echo$server

done

3、遍歷參數列表:

#!/bin/bash

for arg in $*

do

echo$arg

done

4、遍歷文件目錄:

#!/bin/bash

for i in ~/*.sh

do

echo$i

cat$i

done

註:$i是整個目錄和文件,for in語句也可以與` `和$( )配合使用,例如:

#!/bin/bash

for i in $(ls~/*.sh)

do

echo$i

cat$i

done

(1)linuxshellifs擴展閱讀:

LInux shell之(for in)用法總結

一、語法

for 變數名 in 列表 do 程序段(command) done

注意1:是變數名而不是$變數!

注意2:列表可以做文章!

二、應用

第一類:數字性循環-->seq在in後面的應用

#!/bin/bash

#也是產生等差數列-->默認是1

for i in $(seq 1 10)

#產生的是一個字元串,默認IFS是以空格隔開!

do

echo $(expr $i * 3 + 1);

#主要是復習:expr乘法的特殊用法!-->空格隔開

done

補充:產生[1,10]的自然數-->{}在in後面的應用

total=0

#全局變數for i in {1..100} #".."表示連續,默認也是IFS為空格隔開

do

((total+=i))

doneecho -e "total is:${total}"

#多行注釋

<<COMMENRfor i in mysql_{0,1,4,12}sql

#多個文件

do

echo $i

samtools view -c $i

doneCOMMENT

第二類:字元性循環

最原始的

#!/bin/bash

#使用列表for循環顯示周一到周日對應的英文-->學習日期的英文

for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday

do

echo "$day"

done

變數的類型

#!/bin/bash

list="Linux Java C++ Python"

for

i in $list

do

echo -e "Language is ${i}"

done

cat

在in後面的應用-->逐行讀取文件的內容(默認是IFS),所以不是逐行列印。

#!/bin/bashfor

in $(cat 日誌顏色.sh)

#注意:pwd當前目錄下的文件

do

echo $i

done

第三類:路徑查找

ls在in後面的命令是-->讀取當前pwd下的文件(廣義上)。

#!/bin/bash

for i in `ls`;

#ls可以結合統配符應用

do

echo $i is file name! ;

#注意:的應用

done

用通配符讀取目錄(無命令)

for

file in ~/*;

#一級目錄下的內容-->並不遞歸顯示

do

echo $file is file path ! ;

#${file}代表的是文件的全路徑

done

通過腳本傳參

#!/bin/bash

#回憶1:統計腳本參數的個數echo "argument number are $#"!

#回憶2:參數的內容-->此處可以換成$@來測試!

echo

"the input is $*"

#循環執行

for argument in "$*";

do

echo

"$argument "

done

❷ linux shell 如何把txt文本中每一行提取出來賦值給一變數,再輸出這一變數

: No such file or directorybash
你的shell腳本有可能是在windows下編寫的,然後在轉移到linux執行的。
#vi filename
然後用命令
:set ff ---------查看時dos還是unix
若是dos字樣, 那麼你可以用set ff=unix把它強制為unix格式的, 然後存檔退出. 再運行一遍看。

建立腳本最好在linux下,使用touch filename.sh來創建,再用vi filename.sh來編寫腳本命令。
例如:
#touch dd.sh
#vi dd.sh
然後輸入下面的內容。
#!bin/bash
#文本txt每一行復制並輸出----注釋
cat 123.txt|while read var;do
echo $var
done
exit 0

保存後,使用
#bash dd.sh
或者
#chmod 777 ss.sh
#./dd.sh
就可以運行啦。

❸ linux shell 腳本中 如何處理帶空格的超長目錄名 如下:

#/bin/bash
OLDIFS=$IFS
IFS=$'\n'

myhome=`pwd`

for str in `find . -type d `
do
cd $myhome

if [ `expr index "$str" "#"` -eq 0 ]
then
echo "process $str"
cd "$str"
fi
done
IFS=$OLDIFS

❹ linux如何編個shell去指定按照某個文件里列出的文件名來循環刪除某個文件夾下的這些文件

可以的,我給你寫出來吧,用shell ,+741345015

❺ linux shell 如何比較2個文件最後修改時間的大小

僅攻參考
#!/bin/bash -
# 獲取文件列表,並刪除第一行的total ***
filelist=`ls -lt --full-time $* | sed '1d'`
time=()
file=()
#修改shell的欄位分割符
IFS=$'\n'
for f in ${filelist}
do
#取出每行的文件名欄位,放入數組file中
filename=`echo $f | awk '{printf("%s", $9);}'`
file+=($filename)
#取出每行的時間欄位,轉換成EPOCH秒數值(方便比較),放入數組time中
date_modified=`echo $f | awk '{printf("%s %s", $6, $7);}'`
timestr=`date --date="$date_modified" +%s`
time+=($timestr)
done

#取出數組中元素個數,即文件數
#數組file和數組time中每個元素一一對應,即一個文件對應其修改時間值
cnt=${#file[*]}
for((i=0;i<$cnt-1;i++))
do
#如果前者的修改時間大於後者,則輸出提示
if [ ${time[$i]} -gt ${time[$i+1]} ];then
echo "Modification time: ${file[$i]} > ${file[$i+1]}"
fi
done

❻ Linux下的cd`解釋

shell重的`有特殊用法,你輸入的`是單引號還是ESC下面的那個

shell中系統是通過IFS來執行命令的,ifs包括空格,tab建以及回車,,,你要輸入一個命令,shell會默認按照ifs來分隔你的命令

你輸入 cd` 系統會吧`後面的空格或者回車轉義成普通字元,所以shell認為你的命令還沒有結束,所以會成為那樣

你可以試試
cd 'abc
cd 'abc'
cd "abc
cd "abc"

上面的第一個和第三個命令
都是未結束的單引號或者雙引號,這樣shell就處於等待

想了解這方面支持到網上找找shell十三問,仔細看看就明白了

❼ 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: [email protected]
#-----------------------------------
chkUID(){
getUID(){
id|sed -e 's/(.*$//' -e 's/^uid=//'
}

if [ "`getUID`" -ne 0 ]
then
echo -e "\tYou are not root!"
exit 0
fi
}
chkUID
usagePRT(){
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 0
fi

case "$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

❽ 什麼是IFS

盡管現在已經不是一個嚴重的問題,但是 IFS 環境變數曾經在老的 Unix shell 中導致了很多安全問題。 IFS 用來確定命令中什麼樣的分隔詞被發送到原始的 Unix Bourne shell,並與其他環境變數一樣被傳遞下去。通常 IFS 變數應該有一個空格、一個製表符和一個新行的值 -- 這些字元都會被作為一個空格字元來處理。但是攻擊者可以將 IFS 設置為不懷好意的值,例如,他們可能向 IFS 添加一個「/」。這樣,當 shell 試圖運行 /bin/ls 時,老的 shell 將把「/」解釋為一個空格字元 -- 也就是說 shell 將運行「bin」程序(不管在哪兒找到一個),並使用「ls」選項!這樣攻擊者就可以提供一個程序可以找到的「bin」程序。 值得欣慰的是,當今大部分的 shell 都對此進行了防範,當它們啟動時至少會自動重新設置 IFS 變數 -- 包括 GNU bash,GNU/Linux 常用的 shell。GNU bash 還限制了 IFS 的使用,使之只用於擴展的結果。這就意味著減少了 IFS 的使用,而且,這樣危險度也大大降低了 (早期的 sh 使用 IFS 來分離所有的詞,甚至命令)。不幸的是,不是所有的 shell 都可以保護自己( Practical Unix & Internet Security-- 參閱 參考資料 中的鏈接 -- 中有測試這一問題的樣例代碼)。盡管這一特定的問題已經(大部分)可以防範,但它仍以實例證明了沒有經過檢查的環境變數可以帶來難以捉摸的問題。

求採納

❾ linux ifs是什麼

Linux下有一個特殊的環境變數叫做IFS,叫做內部欄位分隔符(internal field separator)。IFS環境變數定義了bash shell用戶欄位分隔符的一系列字元。默認情況下,bash shell會將下面的字元當做欄位分隔符:空格、製表符、換行符。可查看《Linux就該這么學》了解更多Linux介紹。

❿ linux shell編程題目

#!/bin/sh
FILE_NAME="/etc/passwd"
OLD_IFS=$IFS
IFS=":"
while read USR PASSWD USR_ID GRP_ID DESCRIPTION HOME_DIR SHELL
do
if [ "${SHELL}" == "/bin/bash" ]
then
echo "${USR} is bash user"
fi
done < ${FILE_NAME}
IFS=${OLD_IFS}

熱點內容
ins蘋果注冊如何獲取伺服器地址 發布:2024-10-07 22:25:43 瀏覽:626
android怎麼改 發布:2024-10-07 22:24:13 瀏覽:863
我的世界ec伺服器消失了 發布:2024-10-07 21:48:30 瀏覽:661
pythonziptodict 發布:2024-10-07 21:36:09 瀏覽:790
linux操作系統教程 發布:2024-10-07 21:16:54 瀏覽:280
摩托音響怎麼配置 發布:2024-10-07 21:12:37 瀏覽:348
本田思域哪個配置好 發布:2024-10-07 21:04:11 瀏覽:347
演算法期末試卷 發布:2024-10-07 20:51:04 瀏覽:808
編譯期錯誤提示 發布:2024-10-07 20:07:23 瀏覽:297
阿里雲伺服器打開慢 發布:2024-10-07 20:06:33 瀏覽:579