Create dynamical from files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create dynamical from files in directory
# 1  
Old 06-20-2015
Create dynamical from files in directory

Hello

I putting together a menu script for some processing operations and I want to do the following, was googling for some time, but can figure the following out.

Im in a folder lets say /input_files

I want to list the files something like the following, each file is assigned a number, now If I put the corresponding file number to read, I want that the number will point to the file name

Code:
#!/bin/bash
clear
#set -x
echo " TEST"
files=$(ls -l | grep ^- |  awk '{print $NF}')
i='0'
for file in $files
do
i=`expr $i + 1`
echo $i")"  $file
done
echo -e "\n Choose file: "
read VARIABLE_WITH_FILE NUMBER
cat $FILE_NAME

 TEST
1) list_menu.sh
2) one
3) test_file
4) two

 Choose file:
If I press number 3, I want the script to cat test_file, when pressing 1, cat list_menu.sh


Thank you
# 2  
Old 06-20-2015
Try something like:
Code:
select f in $(ls -p | grep -v /)
do
  cat "$f"
done

This would only work if the are no files with spaces or newlines in the directory which may be OK from the command line..

A more robust solution - using bash arrays - would be:

Code:
files=(*)
for ((i=0; i<${#files[@]}; i++))
do
  [ -f "${files[i]}" ] || unset files[i]      # remove directory entries that are not plain files
done

select f in "${files[@]}"; 
do 
  cat "$f"
done


Last edited by Scrutinizer; 06-20-2015 at 08:33 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-21-2015
Thank you so much that select is awesome,

I think the files can be handled much easier -
Code:
ls -l | egrep ^- | egrep -v "*sh" | awk '{print $NF}'


added the PS3 variable + break since after one file I cant to take that value into the next processing.


nevertheless thank you so much

Last edited by Scrutinizer; 06-21-2015 at 06:06 AM.. Reason: CODE tags
# 4  
Old 06-21-2015
Quote:
Originally Posted by kl1ngac1k
Thank you so much that select is awesome,

I think the files can be handled much easier -
Code:
ls -l | egrep ^- | egrep -v "*sh" | awk '{print $NF}'


added the PS3 variable + break since after one file I cant to take that value into the next processing.


nevertheless thank you so much
You're welcome. Yes but it has the same problem, that files with spaces or newlines are not handled properly..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

3. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

4. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

5. AIX

How to create all files generated in a directory with 644 permissions?

Hi, We are using AIX machines. How to create all files generated in a directory with 644 permissions automatically. Regards, Suresh (11 Replies)
Discussion started by: suresh3566
11 Replies

6. UNIX for Dummies Questions & Answers

Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone. I need your help to create a file by using command in Linux. Files names: 0001 More... (4 Replies)
Discussion started by: ggcc
4 Replies

7. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

8. Shell Programming and Scripting

Exporting my dynamical variable won't work?

Even though the idea "might" not be great I still wrote this piece of code to get practice.. Which means that it is the CODE that matters here. Anyways; The intension is to create a program(or do we call it script?) that searches recursively through a folder to find a file - stored in a... (4 Replies)
Discussion started by: Pesk
4 Replies

9. Shell Programming and Scripting

How to create multiple list of files in a directory ?

Hi, i have say 100 files in a directory. file1.log file2.log file3.log file4.log file5.log file6.log ... ... ... file99.log file100.log ========= I need to create another file which contains the list of al these log files. each file should contain only 10 log file names. it shud... (4 Replies)
Discussion started by: robinbannis
4 Replies

10. Programming

how to use a dynamical linked library in C++ program in Linux

I have a dynamically linked library, providing some functions needed in my project. I have successfully imported it into my VC ++ 6.0 project. Now, i am translating the project into pure C++ (such as avoiding using MFC classess) in Linux box. Does anyone know if it makes sense to try to use... (2 Replies)
Discussion started by: cy163
2 Replies
Login or Register to Ask a Question