0

I have this exercise

Write a script which

  • creates files File.txt numer.txt
    • the first contains the list of script arguments, separated by newline,
    • the second contains your UID, if at least one of then already exists display an error message and exit;
  • creates subdirectory C:\WINDOWS in your home and copies to it the above two files;
  • sets the file permissions to files in C:\WINDOWS in such a way that only the user owner and group owner can modify the files, others can only read;
  • creates a symbolic link to /bin in C:\WINDOWS;
  • creates SYSTEM32 file in C:\WINDOWS containing the list of all the files from your home;

and this code

#!/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

Anybody can help with this? I don't know if I solved it correctly and when I run it it always prints 'Error Message'.

11
  • 1
    Are you absolutely sure that that's what it's asking you to do all at once, and in a Bash script? Jun 15, 2019 at 21:41
  • Yes, im sure for that. My teacher asked me abouth that.
    – Ani Hoxha
    Jun 15, 2019 at 21:43
  • Are you running this on a Windows machine with bash? If so, I'd expect it to be difficult to create a directory called C:\WINDOWS under your home directory... In any case, you probably want to tell us what the error message is that you say you get.
    – Kusalananda
    Jun 15, 2019 at 21:45
  • I'm running it on PUTTY, Maybe i have understand it not correctly. If you have any idea please help me :)
    – Ani Hoxha
    Jun 15, 2019 at 21:46
  • The numer.txt exist that's why it's printing Error message (assuming you're running it on Git Bash or any cygwin related applications on windows)
    – ebvjr
    Jun 15, 2019 at 21:49

1 Answer 1

3

There's quite a few mistakes so please excuse me doing this for you. I've commented the changes:

#!/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
5
  • 1
    or printf '%s\n' "$@" >File.txt instead of that loop.
    – Kusalananda
    Jun 15, 2019 at 22:17
  • Thank you so much. Working Perfectly.
    – Ani Hoxha
    Jun 15, 2019 at 22:27
  • 2
    @AniHoxha Good! If this solves your issue, please consider "accepting" the answer. This is the best way to show gratitude on this site. Accepting an answer not only marks the question as resolved, but also signals to future readers that the accepted answer actually solved the issue. More information about this is available here: unix.stackexchange.com/help/someone-answers
    – Kusalananda
    Jun 15, 2019 at 22:38
  • In bash (and plenty of other more "windows native" places) you can use forward slashes for file names when they can't be mistaken for something else.
    – xenoid
    Jun 16, 2019 at 10:01
  • @xenoid In context I believe the OP actually wanted a directory named C:\WINDOWS not a directory called C: containing another directory named WINDOWS. The obscure clue is in the colon (:). No Linux emulators (cygwin etc) that I know of retain the colon and the quote says to create this directory, not simply write into it. Jun 16, 2019 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .