LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-27-2011, 05:16 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
Bash 2 several words filename


Hello guys. I am making a script which one of the functions hide files.
Code:
#!/bin/bash

if [ $1 == "--hide" ]
then

	if [ $2 != "--all" ] 
	then
		files=`echo $@ | awk '{$1=""; print $0}'`
		for i in $files
		do
			if [ -e $i ]
			then
				echo "$i -> .$i"
				mv $i .$i
			fi
		done
	fi
fi
Here is my code but I face with a problem. When filename has few words hidding does not working.
 
Old 11-27-2011, 06:18 AM   #2
sinuhe
Member
 
Registered: Apr 2010
Location: Utah
Distribution: Slackware
Posts: 42

Rep: Reputation: 4
.hidden files

First, you would do better with a case statement instead of if statements. Second, do you mean that when you have spaces in a file name, that your script fails? If so, double quote your expansions, e.g. "$i"
 
Old 11-27-2011, 06:37 AM   #3
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
i tried double quote before but still dos not work.
 
Old 11-27-2011, 07:33 AM   #4
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
maybe any suggestions?
 
Old 11-27-2011, 10:58 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
If file names have spaces in them the for loop will not work as it will break each file into the whitespace separated pieces.

Maybe you could show us how you are calling your script as the format you currently have is a little vague?
 
Old 11-27-2011, 12:00 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Use find(1).
something like this (not tested):

Code:
find . -name "*mask" -exec mv "{};" ."{};"
 
Old 11-27-2011, 12:07 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Yes, more info please. But to start with, check out these links:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

http://mywiki.wooledge.org/BashPitfalls
See 1, 2, 4, 10 in particular. And perhaps 24 and 37.

$(..) is highly recommended over `..`


Have a look at the shift built-in command.

Arrays might possibly be of use too, depending on your exact requirements. See the BashGuide for more on them and other general scripting advice:

http://mywiki.wooledge.org/BashGuide
 
Old 11-27-2011, 02:02 PM   #8
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
i call my script like this
Quote:
~/ScriptDestination --hide filename
 
Old 11-27-2011, 10:12 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well if you are guaranteeing all file names look just like that, I see no issues as script will work without issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: best way to get filename and extension using bash frenchn00b Programming 5 01-09-2010 02:47 PM
bash alias - two words noir911 Linux - General 4 01-22-2009 10:13 AM
how many lines and words (bash) sharapchi Programming 4 12-15-2006 01:45 PM
bash : remove replicator words?, how? Guru Mind Programming 13 05-18-2006 01:24 AM
BASH: First words in a line JordanH Programming 7 10-24-2004 11:00 AM


All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration