LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2010, 07:48 PM   #1
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Rep: Reputation: 0
Using an alias or function without the newline?


I want to create an alias or function that when used prints something like this on the command line so I can further modify it before pressing enter myself.
Code:
$ FILE=exercise1; cc -o $FILE $FILE.c && ./$FILE; FILE=
The idea is that I'm studying c and want to change the name of the file once instead of changing it three times for every compilation and run of the program.

Ideally, after the first execution, the up arrow would show the previous command instead of the alias or function so I don't have to reenter the filename every time. Only when I want to change it.

I've tried a bunch of things in aliases and functions but I don't know how to prevent the newline. My attempts always get printed and I'm back at the prompt and an up arrow(history) shows the previous alias or function, not the full command.

So I guess there's really two questions.

Thank-you for your time.

Solution:
I ended up using this:
Code:
function runc {
  eval cc -o $1 $1.c && ./$1
}
and then calling at the prompt with this:
Code:
$ runc test1
This is file test1

Last edited by gctaylor1; 05-04-2010 at 08:49 PM. Reason: Added solution
 
Old 05-01-2010, 11:29 PM   #2
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
Well assuming you are at the bash prompt, simply put FILE="$1" and then call your script with a single argument
 
1 members found this post helpful.
Old 05-02-2010, 12:41 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Perhaps something like this?

Code:
function runcommand {

# fetch old filename from temporary file
OLDNAME="$(<oldname.txt)"

# prompt to confirm or modify old filename
read -e -p "New file name: " -i "$OLDNAME" FILE

# update temp file
echo "$FILE" >oldname.txt

# verify command to run and edit if necessary.
echo
read -e -p "Command to run: " -i "cc -o $FILE $FILE.c && ./$FILE; FILE=" COMMAND

#run final command
eval "$COMMAND"
}

Last edited by David the H.; 05-02-2010 at 12:59 AM. Reason: modified script to add tempfile
 
1 members found this post helpful.
Old 05-02-2010, 01:53 AM   #4
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
Yes this will work although I would have opted for the one liner:
Code:
eval "cc -o $1 $1.c && ./$1"
Yours is probably better suited if it is to go in a larger script.
 
1 members found this post helpful.
  


Reply

Tags
alias, function, newline



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
Wildcard email alias in /etc/alias file custangro Linux - Enterprise 1 10-02-2009 12:17 PM
[SOLVED] [bash] alias + function = weird... RaptorX Programming 4 08-01-2009 06:36 PM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 12:40 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 09:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 AM.

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