Solved

Getting error when creating a simple alias script

Posted on 2010-09-06
28
472 Views
Last Modified: 2012-05-10
Here is my script but when I run it I get an error

The last bit of code is
Aparently when I run this it says lsf command not found.
Screen-shot-2010-09-06-at-7.43.3.png
0
Comment
Question by:sobeservices2
  • 13
  • 10
  • 5
28 Comments
 
LVL 23

Accepted Solution

by:
savone earned 334 total points
ID: 33614201
Should work, are those backticks or single quotes? You should be using single quotes.

alias lsf='ls -F'

0
 

Author Comment

by:sobeservices2
ID: 33614241
They are single quotes

Then I don't know why its giving me
See screen shot

Screen-shot-2010-09-06-at-8.13.1.png
0
 
LVL 23

Expert Comment

by:savone
ID: 33614270
Try it on the command line just to test.

Then type alias and see if its listed.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:sobeservices2
ID: 33614282
Did that
everything I tried on a command line by line base worked.
When I run it in a script it doesn't can't explain it myself but this was my assignment given to me.

Write a script that does the following and run it from your home directory.  

Use the cat utility to write the contents of file /etc/protocols to a new file called protocolsFile.
Create a new directory called top
Move file protocolsFile to directory top.
Change to directory top.
Display the current directory name
List the files in the current directory.
Use the cat utility to append the contents of file /etc/passwd to file protocolsFile.
Display just the number of lines in file protocolsFile
Display just the number of words in file protocolsFile
Delete file protocolsFile.
Change to your home directory.  Remember your home directory is not /home, it’s the directory you log in to.  (hint: use a command that will work for any user on any system)
Remove the top directory (hint: look at the rmdir command)
Create an alias for the ls –F and call it lsf.
Execute the lsf command.
Start a second shell.
Execute the command that shows all the processes running on that terminal.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33614283
What is the output of

bash -x assignment2
0
 

Author Comment

by:sobeservices2
ID: 33614291
Here is the output of
bash -x assignment2

Screen-shot-2010-09-06-at-8.30.3.png
0
 
LVL 23

Expert Comment

by:savone
ID: 33614316
You may have to make it a global alias
0
 

Author Comment

by:sobeservices2
ID: 33614323
how would i do that?
0
 
LVL 23

Expert Comment

by:savone
ID: 33614339
What distro are you using?
0
 

Author Comment

by:sobeservices2
ID: 33614343
whats is "distro"

I am running Centos OS on one machine and solaris 10 on the other if thats what you mean

using bash
0
 
LVL 23

Expert Comment

by:savone
ID: 33614382


Try this in your script, instead of:

alias lsf='ls -F'

do this:

echo "alias lsf='ls -F'" >> ~/.bashrc
source ~/.bashrc


0
 

Author Comment

by:sobeservices2
ID: 33614414
did not work
same erro line 15 lsf: command not found
0
 
LVL 23

Expert Comment

by:savone
ID: 33614439
Hmmm.. Let me log into my box and try this.
0
 

Author Comment

by:sobeservices2
ID: 33614440
ok will be standing by

0
 
LVL 23

Assisted Solution

by:savone
savone earned 334 total points
ID: 33614484
AHA!



try this:

alias lsf='ls -F'
shopt -s expand_aliases
lsf
0
 
LVL 23

Expert Comment

by:savone
ID: 33614492
I just tested the above and it works great on my fedora box... For an explanation of why this is read the following...

http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/aliases.html

0
 

Author Comment

by:sobeservices2
ID: 33614495
ok that worked
but when I ran it
It followed the script all the way till

#sh
then I had to type in exit
and it then ran the ps
I need it to run the ps inside the sh.

without me having to type in exit
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33614528
Someone beat me to the expand_aliases option.

Why do you need to start a new sh in your script?
0
 

Author Comment

by:sobeservices2
ID: 33614535
it's what my directions call for class assignment
0
 
LVL 23

Expert Comment

by:savone
ID: 33614562
Wow, your really making us work for the 500 points! :)  

This is a good learning exercise, your teacher is really putting you through the paces.  :)

This is not a simple thing to do, I have to log back into my box and give it a go.


0
 
LVL 23

Expert Comment

by:savone
ID: 33614617
Ok, so I tried a few things but the only thing I can come up with it to execute a second shell script.  I am not sure if he wants you to fork processes or if this will be ok with him.

but you can create a second script with just one line in it:

ps

you can name is newshell

then in your first script add this line at the end:

./newshell

This will create a new shell and show the processes.

0
 

Author Comment

by:sobeservices2
ID: 33614624
to create a new shell we had learned

command sh
sh

now when the script runs it runs
and creates the 2nd shell
but I have to type in exit
for the ps command to then exit

last lines of code are
lsf
sh
ps

Open in new window

0
 

Author Comment

by:sobeservices2
ID: 33614631
almost like the script stops at the 2nd shell with the sh

But then I have to type in exit at the 2nd shell for the ps to run

and I think he wants the ps to be running in the 2nd shell

he hasn't talked about creating 2nd scripts and executing a command from one script to the other yet.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33614671
you could always do

lsf
sh -p ps
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 166 total points
ID: 33614673
typo, I meant

sh -c ps
0
 

Author Comment

by:sobeservices2
ID: 33614738
what the -c mean?
Let me try this
0
 

Author Comment

by:sobeservices2
ID: 33614742
Bingo Yeeeeeeeeeee

I finally got it right on

I just need to know what the "sh -c" mean

thanks so much and staying with me on this
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33615010
Type

man bash

to see all the options for bash.

-c means run the following command
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question