bash脚本我正在尝试使用的一行代码

时间:2018-11-22 03:59:14

标签: bash

我有一个文件,将预订存储在其中,并且如果以前有相同的名称,我试图删除旧的预订并放入新的预订。 这行代码对bash有用吗?

sed -i .bak "/^$name|/d" reservation.txt
#!/bin/bash


#start by building a zenity command
ZCMD='/bin/zenity/bin/zenity --title=PA5'
name=`$ZCMD --entry --text="Enter your Name"`

cities=("Portland" "Vancouver" "Seattle" "Riyadh" "Jeddah" ""Las Vegas"" ""New York"")
departCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you departing from?"`

arrivalCity=`$ZCMD --entry --entry-text=${cities[@]} --text="Where are you going to?"`

departDate=`$ZCMD --calendar --text="Choose your departure dates"`
arrivalDate=`$ZCMD --calendar --text="Choose your arrival dates"`

bags=`$ZCMD --entry --entry-text=0 1 2 3 4 5 --text="How many bags are you taking?"`

#Summary of the trip info
echo "Travelers name: $name"
echo "Departure City: $departCity"
echo "Arrival City: $arrivalCity"
echo "Departing Date: $departDate"
echo "Arrival Date: $arrivalDate"
echo "Number of Bags: $bags"

echo "$name|$departCity|$arrivalCity|$departDate|$arrivalDate|$bags" >> reservation.txt
sed -i .bak "/^$name|/d" reservation.txt

1 个答案:

答案 0 :(得分:0)

错误是您添加了一个保留,然后立即将其删除。您应该颠倒最后两个命令的顺序:首先删除任何现有的保留,然后添加新的保留。

也许请确保您正确使用了-i选项-在某些平台上,-i.bak之间不能有空格(而在其他平台上则是必须的)。