壳牌script.Please帮助逻辑错误(UNIX) [英] Logical Error in Shell script.Please help (UNIX)
问题描述
以下是一个源$ C $ C这需要在只有'文件',列出一个文件的文件的权限和通过更换打印输出
R = READ,W-WRITE,X可执行文件。
还应该回声用户但在这里我的问题是,我已经取代了 - '。由用户但如果文件中有R的权限--x,还打印出用户@那point.I知道它不是一个正确的方式去it.Can有人建议我回响着用户更好的办法。
我也尝试循环之前进行打印,但那么它不会成为我的目的,因为我的程序只适合一个文件的枝条文件权限 并没有任何块/插座/管/目录/ etc 的。
#!/斌/庆典 如果[$#-lt 1];然后
回声用法:$ 0文件名
1号出口
科幻
ls -l命令$ 1 |切-C 1-4 | TR\\ 012。 > FP
I = 1 而(($ I< = 4))
做
P ='猫FP |削减-C $ i` 案例$ P在 [dbsplc] | t)的回声不是文件;
出口1 ;;
- )回声用户;;
R)回声READ;;
W)回声写;;
x)的回声执行;; ESAC ((++ i)条)
DONE
退出0
这担任我的目的,我分离的首要条件到一个不同的case语句:
#!/斌/庆典如果[$#-lt 1];然后
回声用法:$ 0文件名
1号出口
科幻
ls -l命令$ 1 |切-C 1-4 | TR\\ 012。 > FP
I = 1
而(($ I == 1))
做
P ='猫FP |削减-C $ i` 案例$ P在 [dbsplc] | t)的回声不是文件;
出口1 ;;
ESAC 回声用户
((++ i)条)
DONE而(($ I< = 4))
做
P ='猫FP |削减-C $ i` 案例$ P在
R)回声READ;;
W)回声写;;
x)的回声执行;; ESAC ((++ i)条)
DONE
退出0
Following is a source code which takes in only 'files',lists the file permissions of a file and prints the output by replacing
r=READ,w-WRITE,x-EXECUTABLE.
It should also echo "User".But the My problem here is that I have replaced '-' by User but then if the file has a permission of r--x,it also prints "User" @ that point.I know its not a correct way to go about it.Can anyone suggest me a better way of echoing "User".
I have also tried printing it before the loop but then it won't serve my purpose, as My program only works withe file permissions of a FILE and not any block/socket/pipe/directory/etc.
#!/bin/bash
if [ $# -lt 1 ];then
echo "USAGE: $0 file-name"
exit 1
fi
ls -l $1 | cut -c1-4 | tr "\012" "." > fp
i=1
while(($i <= 4))
do
p=`cat fp | cut -c$i`
case $p in
[dbsplc] | t) echo "not a file";
exit 1;;
-) echo "User";;
r) echo "READ";;
w) echo "WRITE";;
x) echo "EXECUTE";;
esac
((++i))
done
exit 0
This served my purpose,I separated the first condition into a different case-statement.:
#!/bin/bash
if [ $# -lt 1 ];then
echo "USAGE: $0 file-name"
exit 1
fi
ls -l $1 | cut -c1-4 | tr "\012" "." > fp
i=1
while(($i == 1))
do
p=`cat fp | cut -c$i`
case $p in
[dbsplc] | t) echo "not a file";
exit 1;;
esac
echo "User"
((++i))
done
while(($i <= 4))
do
p=`cat fp | cut -c$i`
case $p in
r) echo "READ";;
w) echo "WRITE";;
x) echo "EXECUTE";;
esac
((++i))
done
exit 0
这篇关于壳牌script.Please帮助逻辑错误(UNIX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!