BASH scripting Question - Select loop displaying output
...
|
Store Output of a Command to an Array
Quote:
Code:
#!/bin/bash Code:
echo ${FILES[$Index]} Storing the output of "ls -1" (you can use any other command, say "ls -ltr") command to an array: Code:
FILES=($(ls -1)) Code:
read -p 'Press the Enter/Return Key to Continue...' Modify the above script to suit your other requirements. Cheers! |
That's a while loop still, I would like it to be a select loop
|
Quote:
|
| more does not will not work inside a select loop.
|
The reason I want a select loop is because eventually I will be running another select loop with functions in that. So the first select loop above will choose a file from the list, then the second loop will execute a function within side of that such as delete.
|
Quote:
To make things more complicated, select will print the options in multiple columns at the slightest provocation: Code:
$ select file in $(seq 1 20); do echo $file; done Code:
dialog --fselect ./ $height $width # will make a nice dialog to select a file |
That is what I am working towards something like this:
INPUT1 = $1 INPUT2 = $2 INPUT3 = $3 VAR1 = 0 LENGTH = 0 if [[ INPUT2 == ""]] then while [ VAR1 = 0 ]; do while [ Array.Length > Length ] do select VAR1 in Array:Length:Length:+19 "sort name" "sort size" "sort age" "more" do case quit) exit;; "sort name") ;; "sort size") ;; "sort age") ;; more) ;; *) esac done length=legnth+19 done done select do #commands in a case delete, copy,etc done fi |
So I want to use the select loops to be able to select an item off the list (a file) then run another select loop and select an item off that list such as delete etc.
|
|
I still don't follow. What is wrong with example given by devUnix? To improve on that, it could look like:
Code:
#!/bin/bash |
I mean I understand that its easier don't get me wrong. I am just working on an objective where it has to be select loops. I can do it in while loops, it's I am not use to select loops.
We need to prompt the user for a file, then they need to select it, then prompt them for a command to use, and both those need to be select loops. We can nest then in other loops. |
sigh. Ok, how about something like this then?
Code:
#!/bin/bash Another problem would be if there happened to be files named "previous" or "next" in the current directory. But I'll leave that to you to deal with as an exercise :P |
I am currently on 2 day order with my Army Unit, havn't had to look at it, just printed it and going to look at it at chow.
Thanks for the help though! |
Quote:
|
hi friends, hope you are doing well...
Guys I wanna tell you that I have a bash script exercise that I need it to be configured for me to pass my scripting class, but the problem is our teacher is bad as shit and he does nothing more than chucking the sheets on us, and whenever you wanna ask him something he goes like "well you are supposed to figure it out yourself" the issue is we are all lost students, and I am trying my best and I bought all the CBT Nuggets scripting videos, but I cant get nothing for now.. and my exercises should be handed over ssh Putty login to Centos server this week, so guys please help me to just pass my subject, and I will defiantly work on CBT Nuggets Videos to learn more and more, I even still needs to do my job work, I work security but human one, not software lol... please Guys help me ... i will type the question soon heaps heap heaps of thanx |
engrmilad, please don't hijack other people's threads. In addition, we're not here to do your work for you.
|
So I have the file working in a function as, is it possible to sort the array as in how next and previous were added, if I wanted to sort by name as a choice as well?
# Start the array. declare -a FILES # Load names into the array, while also dealing with the spaces correctly. for file in *; do FILES+=("$file"); done # Declare varibles -------------------------------------- # Number of files to show at a time FILES_PER_SCREEN=21 # Number of files to move up and down when user selects # "previous" or "next" STEP=22 # The current position in the list -- index of the first filename to display POS=0 # Total number of files in current directory FILE_NUMBER=${#FILES[@]} # Index of the last filename to display ENDPOS=$FILES_PER_SCREEN [[ ENDPOS -ge FILE_NUMBER ]] && (( ENDPOS=$FILE_NUMBER - 1 )) # Store the entire array of available files in a string # If there are files $ENDPOS, include the 'next' option OPTIONS=$(for i in $(seq $POS $ENDPOS); do echo "${FILES[$i]}"; done; (( ($ENDPOS + 1) < FILE_NUMBER )) && echo "next") # To check if the user wants to quit the program QUIT=FALSE # This will treat only newlines as field separators, so the names containing spaces don't break up. IFS=' ' # Main loop =========================================== # will run until user chooses to quit while [ x"$QUIT" = xFALSE ]; do clear # The options select loop ------------------------------- select SELECTED in $OPTIONS; do # Move down in the list if [ x"$SELECTED" = xnext ]; then (( POS = $POS + $STEP )) (( ENDPOS = $FILES_PER_SCREEN + $POS )) [[ ENDPOS -ge FILE_NUMBER ]] && (( ENDPOS=$FILE_NUMBER - 1 )) OPTIONS=$([ "$POS" -gt 0 ] && echo "previous"; for i in $(seq $POS $ENDPOS); do echo "${FILES[$i]}"; done; (( ($ENDPOS + 1) < FILE_NUMBER )) && echo "next") break # Move up in the list elif [ x"$SELECTED" = xprevious ]; then if [ "$POS" -le "$STEP" ]; then POS=0; else (( POS = $POS - $STEP )); fi (( ENDPOS = $FILES_PER_SCREEN + $POS )) [[ ENDPOS -ge FILE_NUMBER ]] && (( ENDPOS=$FILE_NUMBER - 1 )) OPTIONS=$([ "$POS" -gt 0 ] && echo "previous"; for i in $(seq $POS $ENDPOS); do echo "${FILES[$i]}"; done; (( ($ENDPOS + 1) < FILE_NUMBER )) && echo "next") break elif [ x"$SELECTED" = x ]; then break; else # inner select loop ...................................................... select OPERATION in delete move quit; do if [ x"$OPERATION" = x ]; then continue elif [ x"$OPERATION" = xdelete ]; then rm "$SELECTED" elif [ x"$OPERATION" = xquit ]; then exit 0 else echo "$OPERATION" # code to be executed fi done # end of inner select ..................................................... fi done # end of outer select ---------------------------------------------- done # end of main loop =============================================== |
1 Attachment(s)
I uploaded in a txt
|
Quote:
By the way, if you post code, you can enclose it in [code] this is my code [/code] tags to preserve indentation |
That one yes ( but it does not account for numeric sorting), also what about sorting by age and size, then if we wanted to switch back?
|
I know sorting for arrays is a cmd substitution cause with age you add the -t to a ls size -s with ls, just don't know how i would implement it.
|
well the wildcard expansion does not provide info about the size and age of the files, so either you would have to use ls which can sort its output by various criteria, or you would have to get that info by iterating through the array and stating each file. You can add the options to sort the same way as the previous and next entries. Then you would add corresponding code into the select loop to tell the script what to do if the user chooses that option. Basically, it would be enough to just reload the array with
Code:
FILES=($(ls -1)) Note that if there happen to be a file named 'sort by size' etc., you're in trouble. Also note that sorting by age will probably not work with all filesystems, since some linux filesystems don't store the age of files. |
This is what I have managed to get so far. The sorting I had tried to embed into the select loop, but would just be ignored completely.
Code:
#!/bin/bash |
Quote:
I have made some improvements. I put the code to generate the menu items into a function and added some sorting options. I would not recommend adding too many options into the file selection menu or you may end up having 20 options to sort and the filelist listing 3 files at a time :) Code:
#!/bin/bash |
Millgates you rock dude. High Five!
|
All times are GMT -5. The time now is 04:42 AM. |