Question
UNIX Shell Programming This script dirtree.csh which prints a directory tree has some errors: #!/bin/tcsh -f if($#argv == 0) then set thisdir=. else set thisdir=$argv[1]
UNIX Shell Programming
This script dirtree.csh which prints a directory tree has some errors:
#!/bin/tcsh -f
if($#argv == 0) then
set thisdir="."
else
set thisdir=$argv[1]
endif
if($?TREEPREFIX) then
set prefix="$TREEPREFIX"
else
set prefix=""
endif
echo "$prefix|"
set filelist=`ls -A $thisdir`
foreach file ($filelist)
echo "${prefix}|------${file}"
if(-d "$thisdir/$file") then
if($file == $filelist[$#filelist]) then
setenv TREEPREFIX "${prefix} "
else
setenv TREEPREFIX "${prefix}| "
endif
echo $0 "$thisdir/$file"
endif
end
echo "$prefix"
- Study the script and be sure to understand it. explain what it does.
- Execute the script and correct any error it may have to run it. You will have to change permissions to the shell script file to make it executable (remember the chmod command).
- You are required to modify the script, make a copy with the name dirtree_explorer.sh and implement the following changes:
- Create a main menu, so when you execute the script the user will have these options:
The current directory is: /usr/xxx/xxxxxx/
1.Enter a directory name
2.Print the directory tree on terminal
3.Print the directory tree to a file
4.Exit
- When the user runs the script, it will show the home directory at the top of the main menu, unless a directory was passed as an argument to the script, in that case it will show that one as a current directory in the menu
- When the user selects Enter a directory name, the script will capture that into a variable and then it will show that one as a current directory.
- When the user selects Print the directory tree on terminal, the script will print the file system structure under this directory in the form of a tree (as the original script did) and then returns to the main menu.
- When the user selects Print the directory tree to a file, the script will ask the user for the name of the file and then will save the file system structure under this directory in the form of a tree (as the original script did) to that file and then returns to the main menu. You may place the file in the directory where the shell script is. Remember that you may send data to a file and appended with redirection operators (> or >>).
- If the option 4 is selected, the script terminates.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started