Incorrectly using 'ls' in if/then alias


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Incorrectly using 'ls' in if/then alias
# 1  
Old 05-13-2013
Incorrectly using 'ls' in if/then alias

I have an alias defined in which I wish to print the contents of a specific directory using 'ls' based on the if/then condition. The if/then conditions are being correctly evaluated, and the 'ls <path>' result is correct (tested directly in a terminal) although for some reason it seems the 'ls <path>' command isn't be executed. The <path> contents are not displayed. The only "workaround" I've found is to allow the alias to change the directory, and then call 'ls <path>' outside of the if/then logic. The alias is defined as follows (syntactically equivalent example):

Code:
alias mydir  'if ($a == "abc" || $a == "def") then
                        ls /$a/disks/abc/ \
                else if ($a == "ghi" || $a == "jkl") \
                        ls /$a/disks/def/ \
                else \
                        echo "Unrecognized var $a" \
                endif'

Any idea why the contents of the directory are not being listed? Thanks in advance for any suggestions.

/edit: added code tags
# 2  
Old 05-13-2013
Quote:
Originally Posted by coldcoffeecup
I
Any idea why the contents of the directory are not being listed?
Yes: the shell evaluates its command line in consecutive distinct steps. When it expands aliases the phase where variables are expanded is already over. Therefore "$a" doesn't get expanded any more.

The solution to this is "eval", which restarts the commandline evaluation process. Read your shells documentation about "eval".

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 05-14-2013
is it a unix shell script.. if it is then i think syntax for creating alias is

Code:
alias LS='ls -l'

and the if/ then syntax is also different.

Code:
if [ "$a" = "abc" ]; then

elif [ "$b" = "cde" ]; then

else

fi

---------- Post updated at 10:02 AM ---------- Previous update was at 09:54 AM ----------

just try this if it works

Code:
alias mydir='if [ "$a" = "abc" ] || [ "$a" = "def" ]; then
ls /$a/disks/abc/
elif [ "$a" = "ghi" ] || [ "$a" = "jkl" ]; then
ls /$a/disks/def/
else
echo "unrecognized var $a"
fi'

# 4  
Old 05-14-2013
... and a function would be easier anyway.
But the OP uses csh not sh!

---------- Post updated at 08:45 AM ---------- Previous update was at 12:20 AM ----------

csh cannot handle a multi-line string.
A csh alias needs a string.
if-then-else-endif needs multi-line.
So this cannot be done.
If you insist in using csh, your only option is to write an executable script.
Your original code, slightly adjusted:
Code:
#!/bin/csh -f
if ("$1" == "abc" || "$1" == "def") then
  ls /"$1"/disks/abc/
else if ("$1" == "ghi" || "$1" == "jkl") then
  ls /"$1"/disks/def/
else
  echo "Unrecognized var $1"
endif

Save this with name "mydir", make it executable with chmod 755 mydir,
and run it e.g. with ./mydir abc
# 5  
Old 05-14-2013
Quote:
Originally Posted by bakunin
The solution to this is "eval"
Worked simply/perfectly. Thanks!

And thank you everybody else who pitched in =)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ls incorrectly says file not found in sftp.

Hi all When I sftp to an Oracle cloud server, to a folder where there are more than 10k files, and list a small subset of files, it works OK. When I try list more than 10k files, it says file not found. Example output below. (FYI ls/mget with 6k files works OK on this server) Has anyone... (3 Replies)
Discussion started by: pdinsdale
3 Replies

2. Shell Programming and Scripting

Bash string variable outputting incorrectly

Hello All, I am learning BASH scripting and I would appreciate any help with a small problem I am having... I am writing a script that builds a simple hosts file for DNS reasons related to a piece of software called netdb by parsing another application's config files for IP's and their... (4 Replies)
Discussion started by: Wesley545
4 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Strings comparing incorrectly

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far: #!/bin/bash longest="" for i in $(strings -n $1); do if ] then longest=$i fi done echo $longest... (4 Replies)
Discussion started by: SCB
4 Replies

5. Shell Programming and Scripting

Totals in a file - incorrectly displaying

Afternoon, I have a script which creates/modifies data into a formatted csv. The trailer record should display 2 columns, the first is a static entry of "T" to identify it as a trailer record. The 2nd is a total of amounts in a column throughout the entire file. My total isn't displaying... (8 Replies)
Discussion started by: mcclunyboy
8 Replies

6. Forum Support Area for Unregistered Users & Account Problems

For users incorrectly rejected as a spammers

There was a problem with the site servicing our spam address checking. As a result all registrations were being rejected. If you have encountered this problem please try again and you should be permitted to join. (0 Replies)
Discussion started by: reborg
0 Replies

7. UNIX for Dummies Questions & Answers

character displayed incorrectly by ftp

Hi there, I have a ftp server called atlantis. Because of the f...g french characters, I noticed something very weird. Depending on what program I use (ftp, lftp or ssh) the same character is displayed as three different ways. On my local computer, the file is correctly displayed as "modéles".... (1 Reply)
Discussion started by: chebarbudo
1 Replies

8. UNIX for Dummies Questions & Answers

PATH variable set incorrectly?

I've noted that in order to use commands like ifconfig, I have to prefix the commands with the directory. /etc/profile shows that the paths should be part of the PATH environment variable; any idea where the bug is? :confused: # /etc/profile # System wide environment and startup... (1 Reply)
Discussion started by: jon80
1 Replies

9. Linux

Hostname displays incorrectly

hiii, in many of the linux machines i have tried this but the result is the same everywhere. if there are double letters in a hostname then that is displayed as single letter. e.g. if hostname is set to nepttune then login into the shell will display : NEPTUNE(admin)@/ $ hostname... (15 Replies)
Discussion started by: shamik
15 Replies

10. Shell Programming and Scripting

Writing to a log file incorrectly

I have this script: #!/bin/ksh ######### Environment Setup ######### PATH=/gers/nurev/menu/pub/sbin:/gers/nurev/menu/pub/bin:/gers/nurev/menu/pub/mac :/gers/nurev/menu/adm/sbin:/gers/nurev/menu/adm/bin:/gers/nurev/menu/adm/mac:/ge... (5 Replies)
Discussion started by: heprox
5 Replies
Login or Register to Ask a Question