当前位置:首页 » 操作系统 » 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}

热点内容
编译期错误提示 发布:2024-10-07 20:07:23 浏览:296
阿里云服务器打开慢 发布:2024-10-07 20:06:33 浏览:577
磁存储行业的前景 发布:2024-10-07 20:01:29 浏览:548
android对象序列化 发布:2024-10-07 20:01:28 浏览:760
安卓上面的微信如何恢复 发布:2024-10-07 19:57:45 浏览:52
510低配包括哪些配置 发布:2024-10-07 19:38:21 浏览:556
ping服务器地址一直丢包 发布:2024-10-07 19:37:34 浏览:588
怎么去除手机锁屏密码 发布:2024-10-07 18:56:05 浏览:67
怎样注册手机贴吧账号密码是什么 发布:2024-10-07 18:55:17 浏览:365
端口安全配置属于什么 发布:2024-10-07 18:41:01 浏览:794