Help improving .sh script

Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
Every time I open the terminal it gets me to /Users/myuser. Therefore, I'd like to place a .sh file in there and execute it so I won't have to do this manually all the time, and I would need some help with the following, where the comments are, or any other approaches that are better than mine. It is essential to be easy so I can understand and learn from here.

Code:
cd Applications/XAMPP/xamppfiles/htdocs/
wget http://wordpress.org/latest.zip
#wait few minutes OR proceed with the next lines if the above has finished? How do I do that?
unzip latest.zip
cd wordpress
#the following does the job but it throws a - mv: rename .. to ../..: Invalid argument; what is wrong with it?
mv * .* ..
cd ..
rmdir wordpress
#a line to exit the console, is this possible?

Then I would simply run the script, eventually make a shortcut on it on my Desktop. Do I have to chmox the file in order to run it, as in a Linux system? Probably yes, that's a stupid question :).

Thanks.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Sure it's possible, but how often do you actually run this that you want a skript on your desktop? :D

Anyway, in the first part you don't have to solve waiting, the script can just continue after wget finishes.
Worst part is that the downloaded file will always have a different name, so you need to store that in a variable presumably.

After that, I can't make out what you're moving and why :D

Edit: oh sorry, the filename doesn't change, it's really latest.zip each time :-D
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Is this what you're looking for in the end?
mv wordpress/* <location for new files to be replaced>

Edit:
The move command accepts two arguments - first is source and second target. You're passing it 3 and they don't make much sense to me
 
OP
C
Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
Erm, the file will not stay on my desktop but on that folder, in Users. As that, at least on my system, is the path the terminal is opening with :).

Well, the script will definitely wait until wget finishes, or it just can? If the second, how? :) Then I wouldn't need anything else to make it wait, I was thinking of a conditional but will be great if I won't need one.

Then unzip latest.zip would decompress the files in a directory, hence the need to cd wordpress, mv * .. (thanks for that, I've removed the third argument and it works), cd .. and rmdir wordpress.. it would be a lot better to decompress the files straight in that folder, not in another folder.

And the file will always be latest.zip so there wouldn't be any need for a variable.

Thanks for your help and also, how can I do a.. how do I cd out in the tree? The equiv of cd .. and cd .. again? :)
 
OP
C
Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
And yes, I would do this weekly.

Ideally I will add some more commands so when I will execute the script, it will get the latest copy of WordPress as a repo on github, so I can start working on it really quick.

If there's a better approach, do let me know please :).
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Oh that's quite simple, think this is all that should do the trick:
Code:
wget http://wordpress.org/latest.zip
unzip latest.zip
cp -r wordpress/* /Applications/XAMPP/xamppfiles/htdocs/
rm -rf wordpress

You have to copy the files over it turns out, because otherwise mv runs into a "Directory not empty" problem.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Oh and to make that executable at, just make a file with those lines and give it execute permissions, here is an example:
Code:
honza@honzambp$~/temp: cat run.sh 
wget http://wordpress.org/latest.zip
unzip latest.zip
cp -r wordpress/* /Applications/XAMPP/xamppfiles/htdocs/
rm -rf wordpress
honza@honzambp$~/temp: chmod +x run.sh 
honza@honzambp$~/temp: ls -la
total 24
drwxr-xr-x   5 honza  staff   170  4 čvc 16:03 .
drwxr-xr-x@ 81 honza  staff  2754  4 čvc 15:59 ..
-rwxr-xr-x   1 honza  staff    96  4 čvc 15:59 run.sh
honza@honzambp$~/temp:

Edit:
the line adding execute permissions is chmod +x
run.sh is just a filename
 
OP
C
Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
Perfect, thank you.

One more thing, when I run the script with bash filename.sh, can I do something like bash script.sh -varname, where varname to be a variable value from inside the script?

I need so I won't have to manually create the folder first and then run the script, I would just do bash script.sh -foldername. And of course, if is possible, I'd like to learn how to do it too :).
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
You can easily pick up on parameters in a bash script. You can pass nine in total and they are numbered. So, if you want to get the first parameter, you can manipulate the $1 variable. Let's look at an example:

Here I've got a very simple script:
Code:
echo Hello $1
It does nothing but echo back "Hello" and the first parameter sent to the script. If I run the following command (pretend I called it test.sh):
Code:
sh test.sh vansmith
I get the following in response: "Hello vansmith". If you pass it nothing, you would just get "Hello".
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
Nice to keep it simple vansmith :)

Chris, what folder are you creating? The only folder so far is that htdocs, that changes?
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
One more thing, when I run the script with bash filename.sh ...

You don't really need to type "bash" before the script name - of course I'm assuming your default shell is still bash :) But it's your preference, I usually type "./<script_name>" to run whatever script inside the current folder. All up to you though.

Well or ". ./<script_name>" if you ever set variables you want to keep throughout the session.
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Nice to keep it simple vansmith :)
I can make it more complex if you'd like. :p

You don't really need to type "bash" before the script name - of course I'm assuming your default shell is still bash :) But it's your preference, I usually type "./<script_name>" to run whatever script inside the current folder. All up to you though.
You'll need to specify the shell if the script isn't executable. However, if it is, then you certainly don't need to do so. :)
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
I can make it more complex if you'd like. :p

I like simple, but if you have a bunch of free time you'd like to burn through :-D
 
OP
C
Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
You can easily pick up on parameters in a bash script. You can pass nine in total and they are numbered. So, if you want to get the first parameter, you can manipulate the $1 variable. Let's look at an example:

Here I've got a very simple script:
Code:
echo Hello $1
It does nothing but echo back "Hello" and the first parameter sent to the script. If I run the following command (pretend I called it test.sh):
Code:
sh test.sh vansmith
I get the following in response: "Hello vansmith". If you pass it nothing, you would just get "Hello".

Thank you. So if I have FOLDER=$1 in my shell script, I can use ./filename.sh foldername then?
 
OP
C
Joined
Jul 4, 2012
Messages
8
Reaction score
0
Points
1
Nice to keep it simple vansmith :)

Chris, what folder are you creating? The only folder so far is that htdocs, that changes?

Well, I have several folders in htdocs already, their name are the ones from my localhost/foldername. It would be helpful to me that instead of opening the script and change the var value all the time, I will use something like ./wp.sh newfoldername, and a certain variable inside the script will use that parameter as a value.

And yes, ./ works, thanks :).
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top