如何消除命令行中的语法错误?我创建了一个这样的 bash 脚本:
#!/bin/bash
USER="$@"
sed 's/[!@#\$%^&*()]//g'> $@
done
if ! [ -f $@ -a -r $@ ] ; then
echo "Wrong Option : "${USER}" is not readable or not exist" >&2
exit -1
fi
cat "${@}"|while read -r line
do
IFS=, read -r f1 f2 f3 f4 f5 <<< "$line"
mysql --user=user --password=password --database=database <<EOF
INSERT INTO table_name VALUES($f1,'$f2','$f3','$f4',$f5);
EOF
done
我在其他终端上执行这个脚本,如下所示:
$ ./script.sh test!rr.txt
-bash: !rr.txt: event not found
实际上,这是一个将文本文件中的数据插入数据库表的脚本。如您所见,我想在插入文本文件之前检查文件是否存在。
我尝试使用 tr -d 命令和 sed 命令来消除命令行上的语法错误,但我认为它不起作用..
我的期望是:
$ ./script.sh test!rr.txt
Wrong Option : test!rr.txt is not readable or not exist
你有什么主意吗?谢谢。