我试图为学校项目制作一个脚本,但我无法解决这个问题。 我将把我的代码留下评论,希望你能理解我的问题。 提前谢谢。
#!/bin/bash
directory=$HOME/.impressora
imp=0
mkdir -p $directory
#check if $HOME/.impressora exists, if not, create it
if [ ! -f "$directory/.*" ]
# if there is no ".something" file in "$HOME/.impressora"
then echo "" > $directory/.200
# then create a file called ".200" in "$HOME/.impressora"
fi
creditos=$(echo "$directory/*" | cut -f 3 -d '.')
# here is my problem, i want the value of the variable "creditos" to be what comes after the "." on the file in "$HOME/.impressora" , so, in # this case, the file in "$HOME/.impressora" is called ".200" , so the objective is to make "creditos" = 200
# I hope you can help me figure out whats wrong!
if [ $1 = "lp" ]
# if the first variable is "lp" then.
then
if [ -d $directory ] && [ $creditos > 0 ]
#if "$/HOME/.impressora" exists and $creditos > 0
then
echo "" > $directory/lp-$imp
# create an empty file called "lp-$imp" (in wich $imp will be a number)
((imp++))
$cred=$creditos
((cred--))
mv $directory/.$creditos $directory/.$cred
# the objective here is to change the name of the file in "$HOME/.impressora" , so in this case it would go from ".200" to ".199"
# and so on and so on.
else
echo "Diretorio inexistente ou creditos insuficientes"
fi
我发现自己是解决问题的方法!
由于文件没有名称(因为它以点开头),所以 自动显示在您的目录中的第一个文件就是那个。 首先,您需要" ls -a "你的目录。接下来你需要跳过 两个"文件"出现的是:
和
...
所以,你做" 头-3 "命令,所以你可以先选择3 文件夹的行(文件)。接下来,您需要选择最后一个 那些3,所以你再次管它并做" 尾-1 "
最后,您需要删除.200文件中的点,以便保存 只有var的值,为此你需要管道" cut -f2 -d"" "
基本上你需要更换
creditos=$(echo $directory/.* | cut -c7-)
与
creditos=$(ls -a $directory | head -3 | tail -1 | cut -f2 -d".")
答案 0 :(得分:0)
$directory/*
默认情况下仅扩展为非隐藏文件。
如果您执行shopt -s dotglob
,$directory/*
将匹配所有文件(包括.
和..
)。您需要在{。\ n}之后使用shopt -u dotglob
撤消它。
$directory/.*
,您只会匹配隐藏的文件(无论dotglob
)。如果您认为该目录中没有其他内容,则echo .* | cut -c7-
应该为您提供200
。