LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script, display contents of a file using cat (https://www.linuxquestions.org/questions/linux-newbie-8/script-display-contents-of-a-file-using-cat-943897/)

Lindy70 05-08-2012 09:16 AM

Script, display contents of a file using cat
 
This is the script I'm working on, I'm finished except for "displaying a file using cat" I'm unsure of the "wording". I know it's
Code:

cat filename
to view a file. I tried doing
Code:

echo "Enter a filename"
    cat $fname
    more $fname

I can't figure out what I missing.
I'm new to all of this and still tying too figure out all the commands. I would appreciate any insight you can give me.:)








Code:

#!/bin/bash
  leave=no
  while [ $leave = no ]
  do
echo Hello $USER
  cat <<++


          MAIN MENU
 1)  Print current working directory
 2)  Delete a file
 3)  Rename a file
 4)  List all files in a current directory
 5)  Display contents of a file
 6)  Edit a file
 7)  Copy a file
 8)  Move a file
 x)  Exit
++

  echo Please enter your selection $LOGNAME:
  read selection
  case $selection in
  1) pwd;;
  2) echo "Enter any $filename to be deleted"
    read f1
        rm $f1
          echo $f1" is deleted";;
  3) echo "Enter file to to rename"
    read f1
    echo "Enter new name of file"
    read f2
          mv $f1 $f2
          echo $f1" is renamed as "$f2;;
  4) ls -l;;
  5) echo "Enter a filename"
    read $fname
    more $fname
    ;;
  6) echo "enter any file to be edited"
      read f1
            vi $f1;;
  7) echo "Enter $filename to copy"
    read f1
    echo "Enter filename2 to be copied"
    read f2
            #cat $f1 > $f2
                cp $f1 f2
                echo $f2" is copied from "$f1;;
  8) echo "Enter $filename to move"
    read f1
    echo "Enter filename2 where to be moved"
    read f2
          mv $f1 $f2
        echo $f1" has been moved";;
  x) exit;;
  esac
  done
  Exit 0


catkin 05-08-2012 09:36 AM

The problem is the script asks the user to enter the file name but does not read what the user enters. The shell's read command would be useful.

grail 05-08-2012 10:13 AM

As I assume this is homework which is to be marked, may I suggest you try entering a space separated file name for any of your examples and see how your output goes.

Also, you seem to have great trust in your user as you never check that they give you a valid file name, ie. does it exist? can you do the things you want to, to the file?

Just food for thought :)


All times are GMT -5. The time now is 06:22 AM.