1 - Для назначения вывода команды переменной вы можете использовать два метода:
var=$(Your cmd)
var=`Your cmd`
2 - Как вы упоминаете, var
не нужно никаких $
, и знак равенства поставляется с без пробелов.
3 - выходные данные переменной currentfile
находятся в разных строках, и оператор if
не может это проанализировать. Лучше изменить его следующим образом:
if [ -z "$currentfiles" ];then
4 - Также вы должны изменить свой оператор if с условием ИЛИ, например:
if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ];then
5 - then
рядом со строкой 32 отсутствует для секции elif
.
6 - После третьего if
раздела, который идет с elif
, ему нужно fi
, чтобы закрыть if
раздел.
7 - final if
также выглядит как массив, поэтому для проверки, если условие, вы должны сделать что-то вроде этого:
if [ ! -z "$currentfiles" ];then
8 - Ваша переменная currentfiles
содержит номера строк. и перемещаемся в каталог, получая ошибку.
1 file1
2 file2
3 file3
...
Итак, вам нужен только такой список:
currentfiles=$( ls . )
В конце ваш код может быть изменен следующим образом:
#!/bin/bash
subdirectory="Empty_Files"
if [ -f $subdirectory ] # does the Empty_Files file exist?
then
echo $subdirectory "exists!"
else
mkdir -p /home/student/Empty_Files
echo "Empty_Files subdirectory created"
fi
currentfiles=$( ls . ) # check all non hidden files in current directory
for eachfile in $currentfiles
do
checksize=$(du -sh $eachfile | awk '{print $1}')
if [ "$checksize" = "0" ] # check if any files are empty
then
echo -n "Would you like to move the file Y/N:" # if a file is empty ask the user if the want to move the file
read useranswer
fi
if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ]
then
mv $eachfile /home/student/Empty_Files
echo "mv command successful"
elif [ "$useranswer" = "n" ] || [ "$useranswer" = "N" ]
then
echo "File will not be moved"
fi
if [ ! -z "$currentfiles" ]
then
echo "no empty files found in the current directory"
#exit 55
fi
done