Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-31-2014, 10:33 PM   #1
LQ Newbie
Registered: Jan 2014
Posts: 1

Rep: Reputation: Disabled
Using Bash script to create a file and send to $HOME issue


I am fairly new to linux and working on an assignment question for my course work.

I am to " Design a shell script program, creating a mylist.txt containing all file informations in your current directory (i.e., the result of the ls -al command) and move mylist.txt to /home/username/mylist/ (i.e., the username is your user name, and /home/username/ is your home directory). The mylist/ directory will be created automatically."

There are two constraints as well (1) mylist must be given from a standard input (2) The program MUST NOT include /home/username/, so that it will find the home directory automatically.

I have the script working to create the file from the standard input and sending it to the home directory using $HOME but I cannot figure out how to automatically create the mylist directory when doing so. Any help would be appreciated.

My script code so far:


ls -al > $1.txt
mv $1.txt $HOME
Old 01-31-2014, 11:00 PM   #2
John VV
LQ Muse
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,239

Rep: Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527Reputation: 2527
remove that not needed $ in $1.txt
Old 02-01-2014, 02:57 AM   #3
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853Reputation: 4853
I guess with
(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:
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.

Last edited by TobiSGD; 02-01-2014 at 02:58 AM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SFTP file upload bash script issue. moodah Programming 1 10-07-2010 11:55 PM
create a crontab job by bash script issue xiutuo Linux - Server 4 06-07-2010 11:54 PM
Bash script: catch file not found error and send to /dev/null noir911 Programming 7 04-24-2010 09:37 PM
[SOLVED] Need help create a bash script to edit CSV File imkornhulio Programming 13 02-05-2009 11:23 AM
Help with bash script to create directory to /home folder wegadnie Linux - General 5 10-25-2008 12:50 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration