Solved

Getting error when creating a simple alias script

Posted on 2010-09-06
28
466 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now