Page 1 of 1

shell script help

PostPosted: Apr 28th, '13, 20:10
by ofbodur
I am stuck with a question like this..

Write a shell script select loop that lists each file in the current directory and enables the user to view the file by selecting its number.
In addition to listing each file, use the string Exit Program as the sentinel key to exit the loop.
If the user selects an item that is not a regular file, the program should identify the problem as The entered name is not a regular file.
If no input is given, the menu should be redisplayed.

In this problem, I can list each file but, I don't know how to write the code which enables the user to select its number, and the Exit Program is something like
if...
exit o
but I am not sure

Re: shell script help

PostPosted: Apr 28th, '13, 20:42
by doktor5000
What files do you want to loop over? Is it graphics, text, or mixed types of files?

Also, please provide some more context why you want to write something like this.
Imagine a directory with hundreds or thousands of files which you want to loop over, that would be quite
difficult to write, and also not ergonomic and userfriendly.

Re: shell script help

PostPosted: Apr 28th, '13, 20:52
by ofbodur
Think of it like a file which composes of .txt , .c, and .sh files only where a linux beginner works inside.
I have to do it for all files no matter what type of it.

Re: shell script help

PostPosted: Apr 28th, '13, 21:05
by doktor5000
Ok, and how do you select the appropriate viewer for each filetype?
And why can't you use an existing filepicker or file manager? Please provide some more context information what you want to achieve with this.
to help decide, if there are better alternatives or ones which are easier to implement.

Isn't midnight commander (mc) what you want? It can easily be configured to call external programs depending on selected file type.
Two other alternatives would be Last File Manager (lfm -> http://code.google.com/p/lfm/ ) or VI file manager -> http://vifm.sourceforge.net/

Re: shell script help

PostPosted: Apr 28th, '13, 21:10
by ofbodur
just to improve my code writing skills, I want to create my own type.
For the question I asked,
do you have any script which I can write or begin from ?

Re: shell script help

PostPosted: Apr 28th, '13, 21:48
by alf
it's not that difficult and could be made this way:
Code: Select all
#!/bin/bash
cd $1

# get all regular files
  for line in $(find . -type f -name "*.*" -printf '%P\n')
  do
    directory=`dirname "$line"`
    file[$i]=`basename "$line"`
  done

# List the files
  total=${#file[@]}
  for (( i=0; i<=$(( $total -1 )); i++ ))
  do
    if [ $i = 0 ]
    then
      echo ""
      echo "List of files:"
    fi
    echo "$(($i+1))) ${file[$i]}"
  done
  echo ""

 
# Choose which file to open
  while true
  do
    read -e -p "Please choose which file to open or Q to quit and press <enter>: " filechoice
    if [ -z "${filechoice}" ]
    then
        echo "Invalid selection"
   echo ""
    else
   case $filechoice in
       [Qq] ) exit;;
       [$(seq  $total)] ) break;;
       * ) echo $filechoice' Invalid selection';
       echo;;
   esac
    fi
  done
 
  xdg-open ${file[$filechoice]};
 
exit



the directory you want to walk through must be given as cli-option in this script, but be aware it's not tested in any way.

(script source by courtesy of MrsB ;) i assume i was allowed to do ;) )

Re: shell script help

PostPosted: Apr 28th, '13, 22:06
by ofbodur
to list the files in the current directory, which part should I change??
cd $i ?
when I write
cd `pwd`
it does not work...

Re: shell script help

PostPosted: Apr 28th, '13, 22:13
by alf
Code: Select all
<scriptname>  ~
for e.g. the users home-directory.
or if there is a directory "pictures" in user-home:
Code: Select all
<scriptname> /home/<username>/pictures

there are many bash-scripting tutorials like this http://linuxconfig.org/bash-scripting-tutorial to be found in the www.

Re: shell script help

PostPosted: Apr 28th, '13, 22:23
by ofbodur
to make it usable for any computer,
what should I write istead of the second line
cd $1
??
when I write
cd `pwd`
it does not get into the user's current directory.

Re: shell script help

PostPosted: Apr 28th, '13, 22:33
by doktor5000
Don't use backticks, spawns an uncessary subshell. Use $(pwd) instead.
Apart from that it's useless to change into the current directory. Why do you want to do that?

Re: shell script help

PostPosted: Apr 28th, '13, 22:38
by ofbodur
I made it cd $(pwd)
but it gave this
/ÖmerFarukBODURQ5.sh: line 2: cd: /home/omerfarukbodur/Desktop/midterm: No such file or directory

Re: shell script help

PostPosted: Apr 28th, '13, 22:43
by alf
ofbodur wrote:to make it usable for any computer,
what should I write istead of the second line
cd $1
??

Nothing at this point!
You pass the name of the directory as an option when calling the script as i mentioned in my former post.
Please have a look into the countless tutorials about shell-scripting in the www or buy you a book about it, you have to know about the basics if someone should help with any problem.

Re: shell script help

PostPosted: Apr 29th, '13, 15:58
by oj
This is a homework assignment. Same with the other post about writing a script in C. Glad to see the OP is diving into the task head first. Wish I could help but my scripting skills are limited to automating rsync backups...

Re: shell script help

PostPosted: Apr 29th, '13, 16:09
by ofbodur
oj wrote:This is a homework assignment. Same with the other post about writing a script in C. Glad to see the OP is diving into the task head first. Wish I could help but my scripting skills are limited to automating rsync backups...
would you please be clear about what you are saying? In simple words?