basic unix scripting


Recommended Posts

so here's the deal, im trying to learn some basic scripting on a unix system (on my university's system). i am an extreme "newb" as they say to this. im just messing around seeing the different things i can do. ive hit a wall though. i cannot script a change directory command. i dont think there's a syntax problem because this is how i type the command normally... actually ill just put the file contents here:

echo "hello"

cd /user/kellera8/cse232/projects

pwd

those are the three lines of the file.. it executes the echo and pwd commands, but doesn't change directory. can anyone explain why and/or how i can make it change directories. thanks for any help

Link to comment
https://www.neowin.net/forum/topic/448847-basic-unix-scripting/
Share on other sites

I dunno how partition table is set up.

trying to help with what I know.

heres 2 samples one with your method the other with a standard user directory of /home/$USER/

#!/bin/bash

echo "hello"

cd /user/kellera8/cse232/projects

pwd

#!/bin/bash

echo "hello"

cd /home/kellera8/cse232/projects

pwd

also note it would be best to make the sure your script is chmod +x "filename"

You said "unix" server? What shell are you using? If you are using another shell, and bash isn't available, not sure if it will execute properly with #!/bin/bash. I have precious little *nix experience outside of Linux (I have used Solaris and QNX, but not for much besides normal 'user' things).

Well, this is beyond my knowledge, I am afraid.

Are you doing this for a class at your Uni? Your professor can help with your problem. It seems to have something to do with your server connection/settings.

If you are doing this for personal knowledge expansion, you might be better off doing this locally on your PC in Linux. Use a LiveCD if you don't want to install. I can guarantee that it works.

You execute a program from shell, the shell forks another process (another bash) to execute the script and when it has completed the script it will switch back to the original shell hence forgetting what you did during the original batch run.

So there is nothing wrong with your script, you just have to realize that anything you do in a script such as that one, will not affect the main process directly but will rather spawn a new instance of the shell to execute any commands.

I'm not convinced that I can add anything new, but I'll try and give a walk through of what you need to do. You probably know most of this already, but I'll try and be thorough just in case (sorry if it's a bit long).

I assume that you know how to use the terminal to change directory as we need to find out where a few things are. At the terminal type bash and then enter and you'll definitely be running the bash shell which will auto-complete paths for you when you type the start of them and press tab.

Enter the following (not the line numbers) into a file go.sh:

1. #!/bin/csh -f

2. echo "Here"

Line 1 says which shell to use. This is setup to use the C-shell which I'd recommend for scripts such as this (I presume because I started doing this stuff on Solaris). You could use #!/bin/bash instead, the difference is purely in the syntax you need to use, but only for the more advanced features that we're not using here. Note that it doesn't matter what shell you set the script off from, this line will automatically change it to the correct one for your script and change it back again at the end.

Now we need to check that the path /bin/csh actually is correct. If it's not the script won't find the csh executable and so won't work. Go to the terminal and type ls /bin/ and then hit TAB. Is there an entry for csh there? If so all is good. If not we need to look elsewhere. IIRC Linux usually stores its shells in /bin whereas Solaris and other unix's use /usr/bin, so try ls /usr/bin/ TAB. Is csh in there? If so you need to change line 1 of the script to be #!/usr/bin/csh -f. If not then you'll have to look in your OS's documentation to find out where things are stored and change the line appropriately, but I've never heard to them being kept anywhere else.

You should now be ready to run the script. Save it in your home area. On a standard Linux box this will be /home/alex , or whatever your user name is. On a shared box, however, it could be something different, depending on how its setup. For example on one box I have access to my home area is actually /ecslab/uXXajc. In a terminal type cd ~/ ENTER. This will take you to you home area, wherever it actually is. Type pwd ENTER and the terminal should display the path.

The next step is to make the script executable. You should now have a script go.sh in your home area and also a terminal that is working in the home area. Type ls ENTER and, possibly along with some other files, you should see the go.sh script. Now type chmod u+x go.sh ENTER. This allows you to run the script and should only need to be done once.

Now to actually run the script type ./go.sh ENTER. The ./ is required to tell the terminal that the script that the script is in the current working directory, and so it doesn't need to try and find where it is (which uses the $PATH environment variable, but that's a different story).

Upon running the script the terminal should display Here. If not, then your out of luck as I don't think there's much more that can be done remotely from a forum. If so, then all is good. Modify the script to be:

1. #!/bin/csh -f

2. echo "Here"

3. pwd

Save it and use ./go.sh ENTER to re-run the file. Now in addition to displaying Here it should also display the current working directory. If you've forgotten what that is you can just type pwd ENTER at the terminal and the two should be the same.

The final step is to actually change directory in the script. The first thing to do when using hard-coded paths as you want to do is check that the path actually exists. You can't change to a directory that doesn't exist! In a terminal type cd /user/kellera8/cse232/projects ENTER. When I do that on my machine I get a message:

bash: cd: /user/kellera8/cse232/projects: No such file or directory

because it doesn't exist on my machine. There's not much that you can do but play around until you find where the correct directory is. Remember that you can use cd - to go back to your last directory and cd ~/ to go to the home area. Once you have found the area you can type pwd ENTER to give the path. Say this does return /user/kellera8/cse232/projects then change your script to:

1. #!/bin/csh -f

2. echo "Here"

3. pwd

4. cd /user/kellera8/cse232/projects

5. pwd

Note that there isn't anything intrinsically wrong with your current script, it's probably just the one of the paths is wrong. Execute the script and the two pwd commands should give different paths, the second being the one that you entered. Note, however, when the script finished you'll be back in the directory you ran it from. As daPhoenix said a script is essentially a new terminal that runs by itself and is then closed at the end of execution. As a first pass you might want to try trying mkdir test in the terminal which is in you home area. You then know this exists and so the script could be:

1. #!/bin/csh -f

2. echo "Here"

3. pwd

4. cd ~/test

5. pwd

To show the principle of operation.

Hope that helps!

thanks everyone for the responses. thank you ajc4000 for the detailed response. i didn't realize that running a sciprt opened an entirely new shell or process. i believe daPheonix was correct. everything i did in the script worked correctly. i just didn't necessarily see it because the shell returned to the original working directory after the script was done running.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I have the Pixel 9 Pro XL...Unless this thing is "leaps and bounds" faster than the 9, I'll pass. And by leaps and bounds, I don't mean on benchmarks. "Real world" faster. Most people don't even come close to topping out the performance of their phones. Tensor G5 is Google's most powerful chip to date, boasting a staggering 36 percent performance leap over G4.
    • MIT's stunning 'bubble wrap' device squeezes water out from thin air even in deserts by Sayan Sen Image by Matteo Roman via Pexels Massachusetts Institute of Technology (MIT) engineers have built a new kind of device that can pull clean drinking water straight out of the air—no electricity needed. It’s designed for areas where water is scarce and traditional sources like rivers or lakes aren’t reliable. Right now, more than 2.2 billion people globally don’t have access to safe drinking water. In the United States alone, 46 million face water insecurity, with either no running water or water that’s not safe to drink. This new device, called an Atmospheric Water Harvesting Window (AWHW), uses a unique hydrogel panel that looks like black bubble wrap. These dome-shaped bubbles soak up water vapor from the air, especially at night when humidity is higher. During the day, sunlight makes the vapor inside evaporate. That vapor then condenses on a glass surface and drips down through a tube, turning into drinkable water. The AWHW doesn’t rely on power sources like batteries or solar panels. It’s completely passive, meaning it works on its own. The team tested a meter-sized panel in Death Valley, California, one of the driest places in North America, and got between 57.0 and 161.5 milliliters of water per day even with humidity as low as 21 percent. That’s more than what other similar passive devices have managed. “We have built a meter-scale device that we hope to deploy in resource-limited regions, where even a solar cell is not very accessible,” said Xuanhe Zhao, a professor at MIT. “It’s a test of feasibility in scaling up this water harvesting technology. Now people can build it even larger, or make it into parallel panels, to supply drinking water to people and achieve real impact.” Another cool part of the design is how they kept the water safe to drink. Usually, these kinds of hydrogels use salts like lithium chloride to absorb more vapor but that can lead to salt leaking into the water, which isn’t ideal. To solve this, MIT’s team mixed in glycerol, a compound that helps keep salt locked inside the gel. In testing, the lithium ion concentration in the harvested water stayed below 0.06 ppm (parts per million), which is way below the safe limit. The hydrogel domes also give the material more surface area, letting it collect more vapor. The outer glass panel is coated with a special polymer film that helps cool the glass, making it easier for vapor to condense. “This is just a proof-of-concept design, and there are a lot of things we can optimize,” said lead author Chang Liu, now a professor at the National University of Singapore. “For instance, we could have a multipanel design. And we’re working on a next generation of the material to further improve its intrinsic properties.” Published in Nature Water, the study says the AWHW could last at least a year and shows promise for making safe, sustainable water in places with harsh climates. The researchers believe an array of vertical panels could one day supply water to individual households, especially in remote or off-grid locations. Source: MIT News, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • Clear Linux is open source, indeed, so its source code is available for anyone. They're just shutting down its support from them, they're not forbidding anyone else from taking over.
    • Linux Mint is also my favorite distro, but I fear what will happen with it if Clem were to disappear tomorrow, to be honest.
    • Yeah, I totally get your point, which is possible it could happen. I just hope there is a few people around him who are similar to where if they took over things would run pretty much the same. if not, then yeah, it could start to decline rapidly etc. but I figure something that's been around for a longer period of time with a decent backing, and probably more users than most Linux distro's (which I would 'imagine' Mint is one of the more used Linux desktop distro's by volume of people who use it), is less likely to just disappear. but like you said, nothing is guaranteed. but I do think you are probably right in that Clem is probably the core of what keeps Mint, Mint. I like how it tends to stay pretty much the same with some slight tweaks here and there (but is largely the same) instead of that crap some people go for with change for the sake of change trying to create a overly fancy interface and other unnecessary stuff etc. I also feel Mint keeps a nice balance of things out-of-the-box where it's not too bloated, nor too striped down. p.s. but I see Mint as a better Ubuntu basically. but I get your point like if it was more of a really serious choice of needing a 'safe bet' to use long term, then yeah something like official Ubuntu would be one of the better choices for sure given what you said with it being backed by an actual company which makes it a safer bet than Mint which is smaller and 'could' potentially be more fragile.
  • Recent Achievements

    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
    • Explorer
      cekicen went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      209
    3. 3
      Michael Scrip
      202
    4. 4
      Xenon
      145
    5. 5
      +FloatingFatMan
      121
  • Tell a friend

    Love Neowin? Tell a friend!