Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux Bash Complex Variable

Posted on 2008-10-21
2
Medium Priority
?
414 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 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
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.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

877 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