I guess with
Quote:
(1) mylist must be given from a standard input
|
you mean that the name of that file has to be given as a commandline parameter when calling the script. In that case the $1 you use is correct. I also assume that the directory the file should be moved to should have the same name as the commandline parameter given.
Before moving the file create the directory if it does not exist already. You can do that using the mkdir command, but you better use the -p option with it, otherwise it will bail out if that directory already exists.
So your script would look like this:
Code:
#!/bin/bash
ls -al > $1.txt
mkdir -p $HOME/$1
mv $1.txt $HOME/$1/
Keep in mind that there are some implications with writing the script this way:
1. It will not work when run in a directory for that the current user does not have write permissions
2. It will not work correctly if the user does not give the filename as commandline option
3. It may unintentionally overwrite an existing file in case the given filename corresponds with an already existing directory structure
It is likely for now not in the focus of your assignment, but better options to avoid those caveats would be:
1. Directly create the file in the directory you would move it later to anyways
2. Check if the user has given a commandline argument
3. Check if the file already exists and implement a security question, if so
I guess option 2 and 3 will come up later in your course, when you learn if/then statements and reading user input.