Solved

Linux Bash Complex Variable

Posted on 2008-10-21
2
388 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux pull logs and export FTP 4 59
linux installs 6 48
Why isnt it sending mail from my php but is from my server 10 35
linux redhat 7.2 10 38
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now