Lab Exercise 2create a "command" then use chmod, chown and continue working with other Real World commands
create a command - use chmod, chown, which, locate, cnf, zypper, cat, echo and command
Script started on Mon 08 Dec 2014 12:21:15 AM PST
Open a Linux or Cygwin Session and
type the items after the "-->"
(if your output differs then try to figure out why)
-----------------------------------------------
--> mkdir LAB; cd LAB
--> echo "this is a command"
you should see: this is a command
--> echo "this is a command" > tstcmd
--> cat tstcmd
this is a command
If you were to execute "tstcmd" would it do the same thing as the command line above?
In order for it to actually print out the statement, the file tstcmd will need to include the echo and quotes.
the echo command is the executable part of tstcmd to produce the statement.
----------------------------------------------------------------------------
to include the entire string, try:
--> echo "echo \"this is a command\"" > tstcmd
the \ escapes the shell's understanding of the special character " and allows it to be echoed instead of executed
now look at the file again:
--> cat tstcmd
echo "this is a command"
now the file will produce the statement "this is a command" if executed
----------------------------------------------------------------------------
ATTEMPTING TO "RUN" this new program or command called tstcmd.
if you type "tstcmd" at the prompt, what happens?
--> tstcmd
bash: /home/john/tstcmd: Permission denied
what does Permission denied mean?
try it again telling the shell to run it here...
------------------------------------------------
--> sh ./tstcmd
this is a command
that worked...
let's try using command, which ignores the shell functions that we just used.
------------------------------------------------
--> command tstcmd
bash: /home/john/tstcmd: Permission denied
what does Permission denied mean? here it is again
we've created a program that echoes a string, but it's not working.
We mentioned the which command before to tell us where a command was located.
------------------------------------------------
--> which tstcmd
which: no tstcmd in (/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr...
which doesn't find it...
examining our environment variables, let's look at our path statement:
--> env | grep -i path
MANPATH=/usr/local/man:/usr/share/man
XNLSPATH=/usr/share/X11/nls
PATH=/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/bin/:/...
ALSA_CONFIG_PATH=/etc/alsa-pulse.conf
WINDOWPATH=7
QT_PLUGIN_PATH=/home/john/.kde4/lib64/kde4/plugins/:/usr/lib64/kde4/plugins/
the . is in our path but it didn't find it.
we also see other items in our grep... we could clean this up with the following command now that we know the PATH variable is uppercase:
------------------------------------------------
--> env | grep ^PATH=
PATH=/home/john/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/bin/:/home/john/bin:/...
the up caret before PATH and the equal sign after help clearly identify the pattern we're searching for.
The truncated path statement has redundancies, we'll address how to deal with that in a later session.
which didn't find it, how about "locate"
------------------------------------------------
--> locate tstcmd
If 'locate' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf locate
locate is not loaded on SuSe by default, the error message gives us a hint. using sudo we can install it:
------------------------------------------------
--> sudo cnf locate
The program 'locate' can be found in following packages:
* mlocate [ path: /usr/bin/locate, repository: zypp (openSUSE-13.2-0) ]
* mlocate [ path: /usr/bin/locate, repository: zypp (repo-oss) ]
Try installing with:
zypper install mlocate
------------------------------------------------
--> zypper install mlocate
Root privileges are required for installing or uninstalling packages.
------------------------------------------------
--> sudo zypper install mlocate
Retrieving repository 'openSUSE-13.2-Update' metadata ......................................................................[done]
Building repository 'openSUSE-13.2-Update' cache ...........................................................................[done]
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following 2 NEW packages are going to be installed:
mlocate mlocate-lang
The following recommended package was automatically selected:
mlocate-lang
2 new packages to install.
Overall download size: 111.7 KiB. Already cached: 0 B After the operation, additional 389.3 KiB will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package mlocate-0.26-15.1.2.x86_64 (1/2), 62.9 KiB (143.3 KiB unpacked)
Retrieving: mlocate-0.26-15.1.2.x86_64.rpm .................................................................................[done]
Retrieving package mlocate-lang-0.26-15.1.2.noarch (2/2), 48.8 KiB (246.0 KiB unpacked)
Retrieving: mlocate-lang-0.26-15.1.2.noarch.rpm ............................................................................[done]
Checking for file conflicts: ...............................................................................................[done]
(1/2) Installing: mlocate-0.26-15.1.2 ......................................................................................[done]
Additional rpm output:
Updating /etc/sysconfig/locate...
(2/2) Installing: mlocate-lang-0.26-15.1.2 .................................................................................[done]
------------------------------------------------
--> locate tstcmd
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
------------------------------------------------
--> tstcmd
bash: /home/john/tstcmd: Permission denied
clearly we can not execute this file as configured.
Maybe, the file needs to be owned by root, since we're seeing a Permission denied error
--> chown root tstcmd
chown: changing ownership of ‘tstcmd’: Operation not permitted
------------------------------------------------
--> sudo chown root tstcmd
------------------------------------------------
--> ls -l tstcmd
-rw-r--r-- 1 root users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> tstcmd
bash: /home/john/tstcmd: Permission denied
------------------------------------------------
--> sudo chown john tstcmd
changing ownership back to john
------------------------------------------------
--> ls -l tstcmd
-rw-r--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
so after all that, and it still will not execute as a command unless we invoke it from the shell.
we briefly talked about how file permissions had a bearing on which, locate and command and how the file needs to be executable.
------------------------------------------------
--> ls -al tstcmd
-rw-r--r-- 1 john users 18 Dec 8 00:21 tstcmd
the file tstcmd has the following permissions:
owner: read write -
4 2 1
group: read - -
other: read - -
the numeric value for this is 644, where 4+2 =6 or read + write = 6
let's try some changes and see what happens to the file and if it executes
------------------------------------------------
--> chmod u+x tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> which tstcmd
/home/john/tstcmd
------------------------------------------------
--> tstcmd
this is a command
------------------------------------------------
--> chmod 755 tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-x 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod o-x tstcmd
------------------------------------------------
--> tstcmd
this is a command
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod g-x tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwxr--r-- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 706 tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwx---rw- 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 705 tstcmd
------------------------------------------------
--> ls -l tstcmd
-rwx---r-x 1 john users 25 Dec 8 00:23 tstcmd
------------------------------------------------
--> chmod 755 tstcmd
------------------------------------------------
--> ls -al tstcmd
-rwxr-xr-x 1 john users 25 Dec 8 00:23 tstcmd
so we've seen that in order for a command to execute without using the shell function we need to make it executable
with the chmod command. The chown command changed ownership but did not allow it to execute unless the x bit was set.
---------------------------------------------
LINUX Commands - real worldfor the following commands type the example provided, or execute man |
SEARCH and Navigation TOOL |
|
Everett weather -- Seattle - Everett traffic -- assorted News parallel NASB/KJV -- BBC: Middle East South East Asian Missions -- Voice of the Martyrs ![]() Nuts-Bolts-Wrench specs john's vehicle history since 1972 |
|
|