shell script help

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

shell script help

Postby ofbodur » Apr 28th, '13, 20:10

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
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby doktor5000 » Apr 28th, '13, 20:42

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.
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18066
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: shell script help

Postby ofbodur » Apr 28th, '13, 20:52

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.
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby doktor5000 » Apr 28th, '13, 21:05

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/
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18066
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: shell script help

Postby ofbodur » Apr 28th, '13, 21:10

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 ?
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby alf » Apr 28th, '13, 21:48

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 ;) )
Last edited by alf on Apr 28th, '13, 22:07, edited 1 time in total.
for windows problems reboot; for linux problems be root
alf
 
Posts: 326
Joined: Apr 1st, '11, 23:07
Location: DE Paderborn

Re: shell script help

Postby ofbodur » Apr 28th, '13, 22:06

to list the files in the current directory, which part should I change??
cd $i ?
when I write
cd `pwd`
it does not work...
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby alf » Apr 28th, '13, 22:13

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.
for windows problems reboot; for linux problems be root
alf
 
Posts: 326
Joined: Apr 1st, '11, 23:07
Location: DE Paderborn

Re: shell script help

Postby ofbodur » Apr 28th, '13, 22:23

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.
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby doktor5000 » Apr 28th, '13, 22:33

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?
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18066
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany

Re: shell script help

Postby ofbodur » Apr 28th, '13, 22:38

I made it cd $(pwd)
but it gave this
/ÖmerFarukBODURQ5.sh: line 2: cd: /home/omerfarukbodur/Desktop/midterm: No such file or directory
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul

Re: shell script help

Postby alf » Apr 28th, '13, 22:43

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.
for windows problems reboot; for linux problems be root
alf
 
Posts: 326
Joined: Apr 1st, '11, 23:07
Location: DE Paderborn

Re: shell script help

Postby oj » Apr 29th, '13, 15:58

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...
oj
 
Posts: 232
Joined: Aug 23rd, '12, 00:22

Re: shell script help

Postby ofbodur » Apr 29th, '13, 16:09

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?
There is nothing worse than having failed to explain what you believe.
User avatar
ofbodur
 
Posts: 35
Joined: Mar 10th, '13, 12:33
Location: Istanbul


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest