
shell 怎么实现lastindex
1个回答
展开全部
1.分析并执行如下代码
2.请分析代码的具体作用
#/bin/bash
#代码一
function changename() 一{
if [ -z $2 ];then
echo "lack function parameter "
return -1
fi
mv -f $1 $2
return 0
}
function change()
{
ofileName= # old file name
nfileName= #new file name
ls -l > a.txt
index=0
while read line
do
index=`expr ${index} + 1`
if [ ${index} -eq 1 ] ; then
continue; # ignore the first line of 'ls -l'
fi
a=($line)
#echo ${a[8]} the 9th element of array is file name
#echo ${a[3]} # the 3rd element of array is owner name
ofileName=${a[8]}
pos=`expr index ${ofileName} "."` # pos = the . position in file name
if [ ${pos} -eq 0 ] ; then 偿珐稗貉织股半瘫报凯 # if no . in file name then nfileName=oldName+[+owner+]
nfileName="${ofileName}[${a[3]}]"
else
owner="[${a[3]}]."
nfileName="${a[8]/./${owner}} " # substitute the . character to the "[owner]."
fi
echo "nfilename=${nfileName}"
changename ${ofileName} ${nfileName} #call function changename to change file name to new file name
done<a.txt
}
change
ls -l
echo "changed successfully"
错误提示是:
root@ubuntu:/opt/centralManage/shell# ls
sendPic.sh sendVid.sh work.sh
root@ubuntu:/opt/centralManage/shell# sh -x work.sh
+ ScriptSavePos=/opt/centralManage/shell/
+ configpath=/usr/local/bin/authentication
+ crontabfile=crontab.swap
+ read line
+ expr index authentication=0 =
+ len=15
work.sh: 15: work.sh: Bad substitution
root@ubuntu:/opt/centralManage/shell#
说15行有问题,但是周围几行分别是
14 len=`expr index "$line" =`
15 tmpStr=${line:0:len-1}
16 val=${line:len}
问题是:我./work.sh没什么问题啊,可以跑啊,但是sh -x work.sh就报错是为什么?求解答
2.请分析代码的具体作用
#/bin/bash
#代码一
function changename() 一{
if [ -z $2 ];then
echo "lack function parameter "
return -1
fi
mv -f $1 $2
return 0
}
function change()
{
ofileName= # old file name
nfileName= #new file name
ls -l > a.txt
index=0
while read line
do
index=`expr ${index} + 1`
if [ ${index} -eq 1 ] ; then
continue; # ignore the first line of 'ls -l'
fi
a=($line)
#echo ${a[8]} the 9th element of array is file name
#echo ${a[3]} # the 3rd element of array is owner name
ofileName=${a[8]}
pos=`expr index ${ofileName} "."` # pos = the . position in file name
if [ ${pos} -eq 0 ] ; then 偿珐稗貉织股半瘫报凯 # if no . in file name then nfileName=oldName+[+owner+]
nfileName="${ofileName}[${a[3]}]"
else
owner="[${a[3]}]."
nfileName="${a[8]/./${owner}} " # substitute the . character to the "[owner]."
fi
echo "nfilename=${nfileName}"
changename ${ofileName} ${nfileName} #call function changename to change file name to new file name
done<a.txt
}
change
ls -l
echo "changed successfully"
错误提示是:
root@ubuntu:/opt/centralManage/shell# ls
sendPic.sh sendVid.sh work.sh
root@ubuntu:/opt/centralManage/shell# sh -x work.sh
+ ScriptSavePos=/opt/centralManage/shell/
+ configpath=/usr/local/bin/authentication
+ crontabfile=crontab.swap
+ read line
+ expr index authentication=0 =
+ len=15
work.sh: 15: work.sh: Bad substitution
root@ubuntu:/opt/centralManage/shell#
说15行有问题,但是周围几行分别是
14 len=`expr index "$line" =`
15 tmpStr=${line:0:len-1}
16 val=${line:len}
问题是:我./work.sh没什么问题啊,可以跑啊,但是sh -x work.sh就报错是为什么?求解答
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?

2023-06-12 广告
Android和ARM、Linux之间存在密切的联系。Android是一种基于Linux内核的嵌入式智能操作系统,它采用了ARM处理器作为其主要处理器架构。Android的内核和许多应用程序都是基于ARM架构编写的,包括处理器和内存管理器。...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询