Solved

Linux Bash Complex Variable

Posted on 2008-10-21
2
400 Views
Last Modified: 2013-12-16
I would like to store a list of files as a variable using the if statement. What I have below works but its sloppy, is there a better way to do this.

#!/bin/sh
variable=
for file in /directory_path/*.txt; do
if [ -w $file ]; then file2=$file
echo $file2
fi
done
echo $variable
0
Comment
Question by:illtbagu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 22770064
Not much different, but you could do
#!/bin/bash
for file in /directory_path/*.txt
do
  [ -w $file ] && variable="$variable $file"
done
 
echo "$variable"

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22770093
Actually, it's probably best to use find, eg:
#!/bin/bash
variable=$(find "/directory_path/*.txt" -maxdepth 1 -type f -perm +222)

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question