Windows環境でのBashスクリプトの実践

Windows環境でのBashスクリプトの実践

私はこの運動をします。

スクリプトを作成してください。

  • ファイルの作成 File.txt numer.txt
    • 最初に、改行で区切られたスクリプト引数のリストが含まれています。
    • 2 番目には UID が含まれており、そのうちの 1 つ以上がすでに存在する場合はエラーメッセージを表示して終了します。
  • 自宅にサブディレクトリを作成し、上記C:\WINDOWSの2つのファイルをここにコピーします。
  • C:\WINDOWSユーザーの所有者とグループの所有者のみがファイルを変更でき、他の所有者は読み取り専用のファイルのファイル権限を設定します。
  • /binへのシンボリックリンクを作成しますC:\WINDOWS
  • 自宅のすべてのファイルのリストを含むファイルを作成しますSYSTEM32C:\WINDOWS

そしてこのコード

#!/bin/bash

touch File.txt
touch numer.txt
for i in $@
do 
    echo $i >> File.txt
done
id -u >> numer.txt
if $(test -e numer.txt)
then 
    echo Error message
    exit
fi
mkdir C:\WINDOWS
cp File.txt C:\WINDOWS
cp numer.txt C:\WINDOWS
ln -s C:\WINDOWS bin/link
ls $HOME > SYSTEM32

この問題を解決するのに役立つ人はいますか?正しく解決したかどうかはわかりません。実行すると、常に「エラーメッセージ」が印刷されます。

ベストアンサー1

太字がかなり多いのですが、代わりに作って頂いた点ご了承ください。私はこれらの変更について次のように述べました。

#!/bin/bash

# check if files exist and exit 
if [ -f File.txt -o -f numer.txt ] ; then
    echo "Files exist" >&2
    exit 1
fi
## You need this incase there are no arguments
touch File.txt
# but you don't need this
# touch numer.txt

# Always use "$@" not $@, use "$i" not $i
for i in "$@"
do 
    echo "$i" >> File.txt
done
## Really this should be > not >> (you are not appending to an existing)
id -u > numer.txt
# If you test for the file existing after you create it, it will always exist!
#if $(test -e numer.txt)
#then 
#    echo Error message
#    exit
#fi
# \ is the control character to write a single \ use \\
mkdir C:\\WINDOWS
cp File.txt C:\\WINDOWS
cp numer.txt C:\\WINDOWS
# The link should be in C:\WINDOWS and point to bin
ln -s bin C:\\WINDOWS
# one file per line (-1).  And generally use ~ for your home
ls -1 ~ > C:\\WINDOWS/SYSTEM32

おすすめ記事