expanding alias from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting expanding alias from a variable
# 1  
Old 09-29-2009
expanding alias from a variable

Hi !

I am making my first steps to make a script. Therefore i try to make a scp command more easier. Given is the following alias:

14='admin@x-abcd-def.xyz

Now i want to let the script read three var's from the console to use them in the script and then build the scp string.

Code:
echo "Servernumber:";
read srv="";
echo "remotefile incl. abs. path";
read remotefile="";
echo "localfile incl. abs.path"
read localfile

scp admin@$srv:$remotefile $localfile

Doing it this way $srv is not expanded to the value of the alias. expand_alias is set to on in the bash. So where is my mistake?
# 2  
Old 09-29-2009
Code:
#! /bin/bash

echo -n "Server no.:  " ; read server
echo -n "Remote file: " ; read remote
echo -n "Local file:  " ; read local

echo "SCP:> $server $remote $local"

exit 0

Code:
[house@leonov] sh test.bash
Server no.:  12
Remote file: this
Local file:  that
SCP:> 12 this that

# 3  
Old 09-29-2009
Your code isn't consistent even with similar lines. Remove the semi-colons from the end of the lines. Your 'read' lines aren't the same throughout the script either:

Code:
#!/bin/sh

echo "Servernumber:"
read srv
echo "remotefile incl. abs. path"
read remotefile
echo "localfile incl. abs.path"
read localfile

scp admin@$srv:$remotefile $localfile

A good method for testing techniques/syntax is to pull just the technique out into a simple 2-3 line script to validate that the code works and then put it into your script.

Something like:

Code:
read something
echo $something

or

Code:
srv=box
remotefile=/file
localfile=/another/file
scp admin@$srv:$remotefile $localfile

# 4  
Old 09-29-2009
Quote:
Originally Posted by dr.house
Code:
#! /bin/bash

echo -n "Server no.:  " ; read server
echo -n "Remote file: " ; read remote
echo -n "Local file:  " ; read local

echo "SCP:> $server $remote $local"

exit 0

Code:
[house@leonov] sh test.bash
Server no.:  12
Remote file: this
Local file:  that
SCP:> 12 this that

Ok, but this does not expand the alias for the servername.
# 5  
Old 09-29-2009
Where do the aliases get defined?
# 6  
Old 09-29-2009
Quote:
Originally Posted by peterro
Where do the aliases get defined?
~/.bashrc

Works on the console.
# 7  
Old 09-29-2009
That wouldn't be part of your script environment though. Are you using these aliases for something else or mainly this? You could source your .bashrc or keep the definitions in a separate file and source that whenever you have a script that needs to access the definitions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed variable not expanding

I have also some difficulty calling sed to change a word in a file. sed -i 's/docTitl/Outline ${docTitl}/g' $ofln Moved to new thread, since it is a different question (3 Replies)
Discussion started by: Danette
3 Replies

2. Shell Programming and Scripting

Expanding a globed variable name

Heyas I'm trying to give some information on used variables. While the first two work fine, the ones starting with a glob (is that the proper term?) fail. echo ${!TUI_*} ${!RET_*} ${!*_CLI} ${!*\_GUI} bash: ${!*_CLI}: bad substitution Same with @ or have them escaped. I found no... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Quick question on expanding variable

s=`awk '{ print $0}' /Applications/Relink.app/z_cloudline.txt` sed -n '"$s"' /var/mobile/Library/iFile/Bookmarks.plist > /var/mobile/originalip.txt What is the problem with that code ? With variable it only outputs: sed: -e expression #1, char 1: unknown command: `"' If I use the... (3 Replies)
Discussion started by: pasc
3 Replies

4. Shell Programming and Scripting

use variable in alias

Hi Guys, aliase uniq get ut=XXX anistr|availyStus|oratate|uniqueid I want to create aliase but i want xxx as variable..... run like this uniq ak123 uniq ak324 uniq ak123 End of the story i want to use variable in aliase command (5 Replies)
Discussion started by: asavaliya
5 Replies

5. Shell Programming and Scripting

awk alias passing a value to a variable

I am trying to turn this into an alias with no luck. I would then like to put the alias into my bashrc file. I know awk is very picky about quotes. I have tried every version of quotes, single quotes, double quotes, and backslashes that I can think of. VAR=$(xrandr | awk '$2=="connected"{s=$1}... (3 Replies)
Discussion started by: cokedude
3 Replies

6. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

7. UNIX for Dummies Questions & Answers

Passing variable to Alias in Hp kshell

Hi all, I have a series of directories which i open regularly. I want create an alias so that i can pass the direcotry name to alias and then this commands makes Cd to the path i need. COuld you please help on how to create an alias ex of what i am trying but couldn't succeeded #alias... (1 Reply)
Discussion started by: firestar
1 Replies

8. Solaris

Variable not expanding during Solaris pkgadd

I'm having a little trouble with a Solaris package build/install. I have the following entries in my prototype file... # Interfaces file - all versions installed and auto linked to installation type... f none $OPTDIR/config/interfaces.DEV 0444 $OWNER $GROUP f none... (0 Replies)
Discussion started by: JerryHone
0 Replies

9. UNIX for Dummies Questions & Answers

Alias with variable?

I'm new to UNIX. I have to run executables often, and they all have a common prefix "prefix_". Now I'm wondering if I can make an alias where I can type run xyz that will then execute "./prefix_xyz" ? Thanks in advance. (1 Reply)
Discussion started by: JustinT
1 Replies

10. Shell Programming and Scripting

Expanding shell variable

I have a question about expanding shell variables. Given the following piece of script: a="Some text" b="Other text" for i in a b do string1=$i echo $string1 --> returns 'a' string2=EXPRESSION_WITH_$i echo $string2 --> returns 'Some text' done ... (2 Replies)
Discussion started by: lonar
2 Replies
Login or Register to Ask a Question