我是shell脚本新手,我正在尝试备份文件" main.sh"到" main.sh.bak"使用shell脚本。如果文件存在,则执行复制,否则显示消息。下面是我正在尝试的脚本,我需要帮助#34;如果"条件以检查当前目录中是否存在该文件。
#!/bin/bash
echo "enter the file name"
read FILE
if [ -f $FILE ]; then
cp $FILE $FILE.bak
echo "file successfully backed up"
else
echo "file does not exists"
exit 1
fi
exit 0
答案 0 :(得分:2)
现有常规文件的test
命令([ xxx ]
与test xxx
相同)如下所示:
if [ -f $FILE ]; then
<do something>
else
<do something>
fi
重要提示:-f
适用于常规文件,例如,不是符号链接或目录。因此,如果您的文件不是常规文件,请输入man test
以了解有关所有test
选项的更多信息,并根据您的具体情况进行调整。
答案 1 :(得分:1)
您可以使用以下句子检查文件是否存在:
if [ -e $ FILE ]
[ ]
的使用条件与命令test
相同,还有更多选项来检查文件,例如:
-b FILE
FILE exists and is block special
-d FILE
FILE exists and is a directory
-f FILE
FILE exists and is a regular file
此外,请执行man test
查看所有选项
答案 2 :(得分:0)
您的脚本中也存在错误。在<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/round_corner_opacity_background"
android:columnCount="2"
android:rowCount="6">
<!--Pick up-->
<ImageView
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:layout_row="0"
android:layout_rowSpan="2"
android:src="@drawable/ic_ci_pick_up" />
<TextView
style="@style/text_view_description"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:text="@string/pick_up" />
<Button
android:id="@+id/btnSelectPickUp"
style="@style/button_location"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="1"
android:maxLines="3"
android:singleLine="false"
android:text="@string/select_pick_up" />
<!--Separator-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_column="1"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_row="2"
android:background="@color/primary" />
<!--Drop off-->
<ImageView
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:layout_row="3"
android:layout_rowSpan="2"
android:src="@drawable/ic_ci_drop_off" />
<TextView
style="@style/text_view_description"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="3"
android:text="@string/drop_off" />
<Button
android:id="@+id/btnSelectDropOff"
style="@style/button_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="4"
android:ellipsize="end"
android:singleLine="true"
android:text="@string/select_pick_up" />
<!--ConfirmedBookings-->
<Button
android:id="@+id/btnShowConfirmedBookingsDialog"
style="@style/button_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_marginTop="15dp"
android:layout_row="5"
android:text="@string/confirmed_booking" />
</GridLayout>
<Button
android:id="@+id/btnShowBookDialog"
style="@style/button_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="@string/book" />
语句后,您缺少分号。
所以
if
应该成为:
if [ $FILE ] then
答案 3 :(得分:-1)
echo -n "enter file name : "
read file
if [ -e $path/main.sh ]; then
cp /path/main.sh /path/main.bak
echo "Successfully back up"
else
echo "failed"
fi
exit 0;