• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

Fundamental questions on Bash!

Q1.What are the different ways I can tune my scripts to run faster.
Here are a few ways that I know of:
-- use nice -20 myprog.sh to the shell script
-- use xargs instead of -exec in find command i.e. find / -name *.tmp | xargs rm;
-- do not use set -vx myprog.sh

Q2. I want a script to find all occurances of the string "$@" in the "full" man page. and also it should print in what section and page number of the man page you can find "$@"

Q3. Shell supports an exit status ranged from 1-255. Is there any documentation that tells me what does the exit status mean?

Q4.In a shell program I can group some statements in () or {} within the script. While statements grouped  in () round braces do not spawn a child shell, statements sandwitched between {} curly braces are executed in a new shell. Why would I need to execute a group of statements in a new shell in the first place. Any practical examples?

Q5. While an administrator can save his settings in /etc/profile, /etc/profile.d,/etc/bashrc, a normal user can save his setting only in .bash_profile and .bashrc.
Any settings in .bashrc can be inhereted by a child process; however, settings in .bash_profile arn't inherited by child processes. Any examples, which settings I would not like the child process to inherit. In my view one should write all settings in .bashrc because only these environmental settings will be inherited by child processes.

Q6. Where are PS3 and PS4 used? Any practical example.

Q7. What is the difference between less and more

Q8. What is the difference between Extenal and Internal commands in unix. Any practical exmples?

Q9. How does eval work? Any examples.

Q10. What is the practical use of pushd, popd, and dirstack. Any examples?.
I can also use cd to switch between directories and I can also store the directory values in a variabe so that I have a history of all directories the program visited.

Q11.set -o Vs shopt. when should I use set -o and when shopt.
Do they set different settings or they set the same settings?

Q12.Can grep search for files in a directory, or it is used to search for patterns in a file only.
0
gram77
Asked:
gram77
3 Solutions
 
omarfaridCommented:
Hi,

Are you preparing for exam? Any way, not all your questions are related to bash, but to unix OS in general.

Let me answer some of your questions in reverse order:

Q12.Can grep search for files in a directory, or it is used to search for patterns in a file only.

grep can search the content of ascii / text files. Since directories are binary special files, then it can not search in directory for files. Instead use find or ls command.

Q9. How does eval work? Any examples.


eval will evaluate the following line and then execute it:

try the following code in ascript call it myscript:

n=2
eval echo $"$n"
echo $"$n"

run the script as follows:

sh myscript first second

Q8. What is the difference between Extenal and Internal commands in unix. Any practical exmples?

External commands are other programs or scripts that you are calling to run like ls, find, etc.

Internal commands are subroutines within shell that you call e.g. pwd

Q7. What is the difference between less and more

more is a tool to view text (or files) one page a time (like pg) plus some  search capabilities.

for less command please see the link: http://unixhelp.ed.ac.uk/CGI/man-cgi?less

Q6. Where are PS3 and PS4 used? Any practical example.

For the values of PS3 and PS4, please see http://www.gnu.org/software/bash/manual/bashref.html

Q5. While an administrator can save his settings in /etc/profile, /etc/profile.d,/etc/bashrc, .....

Please note that the files in /etc (/etc/profile /etc/bashrc, ...) are system wide files that the administrator can use to initiate certain variables, etc for all users on the system. They are not particular to the admin user.

The variables set in those files can be exported or not exported. Exported variables can be see by child processes, while variables that are not exported are not seen by child processes.

The individual user can set user specific variables the same way in .profile and .bashrc in home dir.

The difference between .profile and .bashrc is that .profile is read and executed once by the login shell, while .bashrc is read and executed with each call to a child shell.




0
 
TintinCommented:
Q1.What are the different ways I can tune my scripts to run faster.
Write them in Perl or C ;-)

Q2. I want a script to find all occurances of the string "$@" in the "full" man page. and also it should print in what section and page number of the man page you can find "$@"

#!/bin/sh
IFS=":"
for path in $MANPATH
do
  find $path -type f -exec fgrep -l '$@' {} \;
done


Q3. Shell supports an exit status ranged from 1-255. Is there any documentation that tells me what does the exit status mean?

0 = success
>=1 means failure

Q4.In a shell program I can group some statements in () or {} within the script. While statements grouped  in () round braces do not spawn a child shell, statements sandwitched between {} curly braces are executed in a new shell. Why would I need to execute a group of statements in a new shell in the first place. Any practical examples?

Running a block in a child shell won't affect any settings in the parent.

Q5. While an administrator can save his settings in /etc/profile, /etc/profile.d,/etc/bashrc, a normal user can save his setting only in .bash_profile and .bashrc.
Any settings in .bashrc can be inhereted by a child process; however, settings in .bash_profile arn't inherited by child processes. Any examples, which settings I would not like the child process to inherit. In my view one should write all settings in .bashrc because only these environmental settings will be inherited by child processes.

Q6. Where are PS3 and PS4 used? Any practical example.

#!/bin/bash
export PS3='Select Option: '
select name in a b c
do
  echo $name
done


export PS4='DEBUG> '
bash -x scriptname

Q7. What is the difference between less and more

less is more and more is less.  Both are similar, but less offers more functionality than more.

Q8. What is the difference between Extenal and Internal commands in unix. Any practical exmples?

By "internal commands", what you really mean is shell builtins.  Shell builtins are commands/functions built into the shell and don't require forking a new process.  eg: cd, pwd

Q9. How does eval work? Any examples.

omarfarid gave an example.

Q10. What is the practical use of pushd, popd, and dirstack. Any examples?.
I can also use cd to switch between directories and I can also store the directory values in a variabe so that I have a history of all directories the program visited.

Q11.set -o Vs shopt. when should I use set -o and when shopt.
Do they set different settings or they set the same settings?

Q12.Can grep search for files in a directory, or it is used to search for patterns in a file only.

Depends if you literally mean a directory or files in a directory.  There's no point in using grep on the actual directory itself, best to do

grep 'string' directory/*
0
 
Duncan RoeSoftware DeveloperCommented:
eval is slow!!! Better use Bash internal arithmetic. See "man bash".
There are no built-in commands in unix, only in programs run by it. Shells (e.g. bash) tend to have a number of these, for efficiency / consistency. Any program that wants to be able to be told to change directory must implement its own cd command. On the other hand, pwd can be external (it's a bash builtin but on my system I can explicitly invoke /bin/pwd).
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now