LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-27-2018, 05:51 PM   #1
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 8

Rep: Reputation: Disabled
Creating named text file using input with alias


Hi,
I hope someone here can help.
I'm trying to write a script/alias that creates a text file called whatever I input(in a particular set location in Documents -for example).

I know I can just use "touch filename.txt" but I'm looking for something more challenging to learn from as I can't find suitable examples where an argument is used as the input.
Everything I find is Hello world, Echo this etc.

For example the Alias is called blankfile

When I type> blankfile newfile(or whatever I want). It creates this named file in Documents.

Thanks in advance for any useful input!
 
Old 04-27-2018, 08:39 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
do yo mean something like this?
Code:
#!/bin/bash
read -p  "what name file you want? " name
touch $name
 
1 members found this post helpful.
Old 04-27-2018, 09:03 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Or, more basic
Code:
#!/bin/bash
# createafile, to create a file

touch $1
Then run it as
Code:
$ createafile filename
 
1 members found this post helpful.
Old 04-28-2018, 03:21 AM   #4
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 8

Original Poster
Rep: Reputation: Disabled
Talking

Thanks for the feedback guys, very helpful!
I have it almost the way I'd like it.

1) Currently the script doesn't save to Documents (by default). If I specify the folder path to the script then this works.
Code:
alias createfile="/home/myuser/Documents/createafile.sh"
createfile mynewfile

2) Is it possible to avoid using a separate script and have it included in it's own command(for example using &&)?

I've tried using:

alias createfile2=touch /home/myuser/Documents/$1
and
alias createfile2=touch "/home/myuser/Documents/$1"

but when executing createfile2 mynewfile, it just creates the file in the pwd and not Documents(by default).

Once again, thanks for the assistance so far!
 
Old 04-28-2018, 06:13 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
From
https://ss64.com/bash/alias.html

Quote:
The value cannot contain any positional parameters ($1 etc), if you need to do that use a shell function instead.
OK
 
1 members found this post helpful.
Old 04-28-2018, 06:45 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
yes, an alias cannot do this.
it would be better to write a short script, make it executable, and place it in $HOME/bin (assuming that's in your PATH).
or a function, can do the same.

Code:
#!/bin/sh

cat <<EOF > "$HOME/Documents/$1"
#!/bin/sh

exit 0
EOF

chmod +x "$HOME/Documents/$1"
 
Old 04-28-2018, 11:26 AM   #7
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ok thanks all who have helped!!!

Looks like it script it has to be.
 
Old 04-28-2018, 12:00 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by justme48 View Post
Ok thanks all who have helped!!!

Looks like it script it has to be.
this will put the file in your current directory
Code:
alias makefile='touch $1'
Or create a function within your .bashrc
Code:
makefile()
{
  file="/home/userx/Documents"
  cd $file
  touch $1
  cd
}
because touch creates the file in the current working directory. Then just type in the function name and file name then it does what it is told to do.

You said script so that's where my mind went to the first time.

Remember to open up a new terminal each time you make changes to your .bashrc to update the terminal for testing your changes.

All you need to do is figure out how to get it back to where you where at, if you where not in your parent home dir working in the terminal, that is simple enough, yes? .

Last edited by BW-userx; 04-28-2018 at 12:29 PM.
 
1 members found this post helpful.
Old 04-28-2018, 04:15 PM   #9
justme48
LQ Newbie
 
Registered: Apr 2018
Location: London
Distribution: CentOS 7
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks BW-userx.

Perfect! I think this is the closest to what I'm looking for. Although I found an alternative script method(with help above from Awesome Machine and your previous input).

At least I've learnt something from your assistance(and everyone) and now aware of a few ways to accomplish the task.


****************************
My main goal is to write a multi-function script(with some kind of menu-maybe Dialogue) which can handle basic admin tasks on a remote linux pc.(add printer- [several] and copy driver over), check for and delete lock files with Firefox etc etc, maybe backup bookmarks file etc etc.
I know how to do these tasks on an individual basis but stringing them in to a menu is going to be some challenge.(in another post - if I need it ?!?).
 
Old 04-28-2018, 04:38 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by justme48 View Post
Thanks BW-userx.

Perfect! I think this is the closest to what I'm looking for. Although I found an alternative script method(with help above from Awesome Machine and your previous input).

At least I've learnt something from your assistance(and everyone) and now aware of a few ways to accomplish the task.


****************************
My main goal is to write a multi-function script(with some kind of menu-maybe Dialogue) which can handle basic admin tasks on a remote linux pc.(add printer- [several] and copy driver over), check for and delete lock files with Firefox etc etc, maybe backup bookmarks file etc etc.
I know how to do these tasks on an individual basis but stringing them in to a menu is going to be some challenge.(in another post - if I need it ?!?).
my pleasure, do not forget to click
Did you find this post helpful? Yes <-- the yes if you found it helpful.

enjoy!
 
Old 04-29-2018, 02:55 AM   #11
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I think it might also help to add that arguments are separated by whitespace, and are numbered $1, $2, $3, etc. So, in
Code:
$ command argument1 argument2 argument3
$1=argument1, $2=argument2, and $3=argument3. And you can have more arguments.
 
Old 04-29-2018, 02:12 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by AwesomeMachine View Post
I think it might also help to add that arguments are separated by whitespace, and are numbered $1, $2, $3, etc. So, in
Code:
$ command argument1 argument2 argument3
$1=argument1, $2=argument2, and $3=argument3. And you can have more arguments.
wouldn't using the $1 $2 $3 assignment to a different value mess up the information being gotten off of the command line?
Code:
#!/bin/bash

#argument numbers
echo $1
echo $2
echo $3
echo
#number of arguments passed to a shell script
echo $#
#
echo;echo
# the arguments given in sequence 
echo $@
maybe you just wrote it back words ( backwards ).

Code:
$1=argument1, $2=argument2, and $3=argument3

argument1=$1
argument2=$2
argument3=$3

Last edited by BW-userx; 04-29-2018 at 02:14 PM.
 
Old 04-30-2018, 01:18 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious how any of the solutions are any better than the original touch command? Seems a lot of effort to re-create this wheel.
Code:
touch ~/Documents/filename
Whilst a whopping 13 characters extra, I don't see much gain in the scripts

For those who might come back with, "but what about multiple files?" ... you simply add {} and commas and you're set
 
Old 04-30-2018, 08:01 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
I am curious how any of the solutions are any better than the original touch command? Seems a lot of effort to re-create this wheel.
Code:
touch ~/Documents/filename
Whilst a whopping 13 characters extra, I don't see much gain in the scripts

For those who might come back with, "but what about multiple files?" ... you simply add {} and commas and you're set
OP is just trying to get some experience under his belt

quote:
"I know I can just use "touch filename.txt" but I'm looking for something more challenging to learn from as I can't find suitable examples ..."
 
Old 04-30-2018, 07:46 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by BW-userx View Post
OP is just trying to get some experience under his belt

quote:
"I know I can just use "touch filename.txt" but I'm looking for something more challenging to learn from as I can't find suitable examples ..."
My bad, I did miss that part while looking at what everyone was providing :P

In that case. my contribution would be:
Code:
touchd()
{
  for name
  do  
    touch "$HOME/Documents/$name"
  done
}
This can be used with one or multiple files and as long as the file name is in quotes, it can have white space too
 
  


Reply



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
using bash to input text into text file speedmonkey Programming 7 12-20-2017 01:02 AM
Creating user accounts from a csv file, force to change password and create alias rojasm Linux - Newbie 13 04-02-2013 10:19 PM
[SOLVED] feed text to a file editor from a named pipe kdelover Linux - Newbie 5 04-24-2011 03:20 PM
file /var/lib/named/var/named/reverse/named.zero failed: file not found Toadman Linux - Software 15 03-18-2009 07:01 PM
cp command with input from a text file khaos83 Linux - Newbie 3 08-08-2008 06:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 PM.

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