1 Replies - 7144 Views - Last Post: 19 March 2013 - 12:29 PM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Extending my bash script to recieve more parameters?

Posted 19 March 2013 - 11:20 AM

I have the following bash script, which is called trash.sh. In my script I request one parameter for from the user, simply a file name. And move the file to the Trash folder which is located in the user's home directory. If the directory doesn't exist, it simply creates one and then moves the file there. On the other hand, if the file doesn't exist, it informs the user.

#!/bin/bash

FILE=$1
FOLDER="$HOME/Trash"
ARGS=1

if [ $# -ne $ARGS ]
then
	echo "Error: You are missing an argument!"
	echo "Usage: ./trash.sh <file_name>"
else
	if [ -s $FILE ]
	then
		if [ -d $FOLDER ]
		then
			mv -v $FILE $FOLDER
		else
			mkdir $FOLDER
			mv -v $FILE $FOLDER
	  	fi
	else
		echo "The file you have entered does not exist!"
	fi
fi



Now, I want to extend my script in the followign ways, but I don't know how, because I am not that much experienced with bash scripting. First of all, I want to let the user enter more than one parameters, simply more then one file name, and move all the files in the Trash folder. If one or more of the files don't exist, it will inform the user which file or files don't exist. Simply, I want my script to recieve as many parameters as the user wants.

For example, if the user calls the script like this:

./trash.sh file1 file2 file3



and let's say that file2 doesn't exist I want the output to be.

file1 -> /home/user/Trash/file1
file3 -> /home/user/Trash/file3
file2 doesn't exist!



And lastly, I also want it to accept a parameter like this:

./trash *.txt



Simply, which will move all the files that end with .txt extension. If someone could help me achieve those things/extend my script, I would be glad.

Is This A Good Question/Topic? 0
  • +

Replies To: Extending my bash script to recieve more parameters?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,474
  • Joined: 23-August 08

Re: Extending my bash script to recieve more parameters?

Posted 19 March 2013 - 12:29 PM

Here's your manual.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1