Solved

Linux Bash Complex Variable

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

12 Experts available now in Live!

Get 1:1 Help Now