我有以下 if-else 语句,用于检查 i 标志是否已添加到命令中,如果是,则从 stdin 写入文件:
29 if [ $inplace -eq 1 ]; then
30 tmp_file=$(mktemp)
31 awk -v num="^$number" -v col="$column" -v value="$value" -F ',' \
32 'BEGIN{OFS=FS}{ if ($0 ~ num) { $col=value; print } else print}' "$file" > "$tmp_file"
33 mv "$tmp_file" "$file"
34 else
35 awk -v num="^$number" -v col="$column" -v value="$value" -F ',' \
36 'BEGIN{OFS=FS}{ if ($0 ~ num) { $col=value; print } else print}' "$file"
37 fi
但我收到以下错误:
mv: cannot move '/tmp/tmp.ahALZC2Hgh' to '': No such file or directory
这意味着 $file 是空的,这对我来说很有意义,因为我在某处读到 stdin 只能使用一次并且它被上面的 awk cmd 使用
有人知道如何保值吗?(不做类似: var=$(cat) 的事情,因为我听说这是不好的做法?)
或者,如果这是一个坏主意,因为并且不应该实施,那也可以!
运行该文件并收到错误的命令是:
cat data.csv | ./update.sh -n 444 -c 2 -v "Alex" -i
i 标志用于用户表示用户想要对原始文件进行更改,在这种情况下,变量 $inplace 在以下情况的 while 循环中设置为 1:
3 while getopts f:n:c:v:i opt; do
4 case $opt in
5 f) file="$OPTARG";;
6 n) number="$OPTARG";;
7 c) column="$OPTARG";;
8 v) value="$OPTARG";;
9 i) inplace=1;;
10 *) printf>&2 '%s\n' "Usage: $0 [-f file] [-n number] [-c column] [-v value]";;
11 esac
12 done
整个脚本如下:
#!/bin/bash
while getopts f:n:c:v:i opt; do
case $opt in
f) file="$OPTARG";;
n) number="$OPTARG";;
c) column="$OPTARG";;
v) value="$OPTARG";;
i) inplace=1;;
*) printf>&2 '%s\n' "Usage: $0 [-f file] [-n number] [-c column] [-v value]";;
esac
done
if [ -z "$column" ]; then
echo "Please specify a column number with -c." >&2
exit 1
elif [ -z "$number" ]; then
echo "Please specify a phone number to filter by via -n" >&2
exit 1
elif [ -z "$value" ]; then
echo "Please specify the new value via -v" >&2
exit 1
elif [ "$column" -gt 4 ] || [ "$column" -lt 1 ]; then
echo "this script can only handle cols1-4. for group editing, try ./groups.sh" >&2
exit 1
fi
if [ $inplace -eq 1 ]; then
tmp_file=$(mktemp)
awk -v num="^$number" -v col="$column" -v value="$value" -F ',' \
'BEGIN{OFS=FS}{ if ($0 ~ num) { $col=value; print } else print}' "$file" > "$tmp_file"
mv "$tmp_file" "$file"
else
awk -v num="^$number" -v col="$column" -v value="$value" -F ',' \
'BEGIN{OFS=FS}{ if ($0 ~ num) { $col=value; print } else print}' "$file"
fi
exit 0
查看您收到的评论,然后尝试将此(未经测试)作为起点:
您应该对选项参数进行更多验证,以确保
column
您期望的自然数之类的值确实是自然数。