#!bin/bash#默认分隔符为空格或换行或制表
File="test"cat$Filefor state in$(cat $File)doecho"$state"done
//
test文件内容:
this is first line !
输出
this
is
first
line
!
实战三:从文件中读取一行行数据
#!bin/bash
File="test"echo"the content of test:"cat$Fileecho"the result:"
IFS=$'\n'#设置分隔符为换行符for state in$(cat $File)doecho"$state"done
输出结果:
the content of test:
this is first line !
this is second line !
this is third line !
the result:
this is first line !
this is second line !
this is third line !
实战三:从文件/etc/passwd文件中以冒号,换行为分隔符读取数据
#!bin/bash
File="/etc/passwd"echo"the content of $File:"cat$Fileecho"the result:"
IFS=$'\n':#设置分隔符为换行,冒号for state in$(cat $File)doecho"$state"done
实战四:读取用户名和密码,创建系统账户
#!bin/bash#read user's name and password from standard inputread -p "请输入用户名:" user#display 请输入用户名:read -s -p "请输入密码:" pass#-s表示用户输入的任何数据都不显示useradd"$user"#创建用户
echo"$pass"|passwd"$user"#使用了管道,左边从管道输入数据,右边从管道获取数据