我的 bash 腳本中的以下行
cat >> $file_name
給我這個錯誤:
./l7.sh: 第 12 行: $file_name: 不明確的重定向
這是完整的代碼
https://github.com/vats147/public/blob/main/l7.sh
為什么?
uj5u.com熱心網友回復:
到file_name您必須分配的引數中$1,它將作為輸入引數傳遞給當前檔案。
#! /bin/bash
echo -e " Enter file name : \c"
read file_name=$1
if [ -f $file_name ]
then
if [ -w $file_name ]
then
echo " type some text data. to quit press enter "
#cat > $file_name(single angular bracket use for overwritten)
#cat >> $file_name(two angular bracket use for appending a text)
cat >> $file_name
else
echo " file not have write permission"
fi
else
echo "file not exist"
fi
這些是腳本的位置引數。
執行./script.sh Hello World將使
$0 = ./script.sh
$1 = Hello
$2 = World
筆記
如果你執行./script.sh,$0將給出輸出,./script.sh但如果你用 bash 執行它,script.sh它將給出輸出script.sh。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/367776.html
