Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I'm making a rather big (at least i think so) shell script which includes a host side and a target side. The host being an actual computer running Linux/various distros and the target being a DevKit8000 (almost the same as a beagleboard).
The DevKit8000 runs on kernel version 3.2.6.

But there are a few things which i haven't been able to do so far, so i'm hoping some of you can help me out.

1) I would like to have all the variables that can be changed by the user in 1 file called "settings", so it's easier for the user to edit 1 file instead of multiple files.
I've tried to use 'export' to mark variables, then run the script just before running my main script, but the main script can't find the exported variables no matter what i do?

2) Is it possible to change hardcoded variables while inside a script? I have some variables that link to files, and i would like for the user to be able to change the name of the files while inside the script, so he wont have to open the file up in a text editor to manually edit them.

3) I've trapped CTRL+C so it restarts my script instead of exitting it.
Is there any way to simulate the keypress CTRL+C in code?

4) Is there a way to make a script non-executable when done manually by the user, but remain executable when executed from another script?
I have several scripts, and i only want the user to be able to execute the "main" script, while the "main" script can still execute all the sub-scripts.

5) My target-side script involves inserting/removing linux kernel modules, and some of these modules have printk functions in them, with a log level high enough to be printed to the screen.
This doesn't look very good if it prints out messages in the middle of my script. I wish to suppress the messages so they ONLY show up when using 'dmesg' and dont get printed out to the screen.
I was thinking about piping them out to /dev/null, but not sure how about to do it for printk (seeing as it isn't actually inside my script), and would that cause them to not show up in 'dmesg' ?

6) How can i check the return values/error returns for functions? For example when i use 'ssh user@target' and it can't connect for some reason. How can i check what the error message was and do something based on that info?
I would like to simply echo something to the user if ssh fails and then not continue with the rest of the script.

7) I've used the 'resize' function on my host machine script to resize the terminal window so the entire menu can fit on there. That's fine and it works, but my problem is that on my target, the 'resize' function is non-existant.
So if the target script is called, i can't resize the window, meaning the menu could possibly look distorted and ugly.
Is there any method that works like 'resize', or would it be possible for me to add 'resize' to my target?

8) Same as the above with 'resize', but this one is for '$LINENO'. It is available on my host machine, but not on my target.
Any other command that works like it, or can i add it manually?

9) A picture is worth 1000 words:
I would like to fix it, so if a word is seperated at the end of the terminal window, it just jumps down to the next line.
That way it looks much better.

I know it's a lot of questions in 1 post, but would rather just add them all up here instead of making more posts.

I would love some help, even if it's only with a few of the questions :)

share|improve this question

closed as too broad by Sylvain Pineau, guntbert, Andrea Corbellini, Florian Diesch, Eric Carvalho Jun 13 '14 at 13:05

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

I think you should split your question into individual questions for each problem and post the programming related ones over at SO. Most of them are pretty basic stuff, so you should rather search for an answer before posting them anywhere. –  David Foerster Oct 13 '13 at 18:56

1 Answer 1

1) To load in variables from another file you need to "dot" or "source" that file, not run it. If you run a script a subshell is created which will have those variables, but then when it ends the shell is destroyed and settings lost. A variable file like this should not have a #!/bin/bash line in it

Dot a file like this: . my_variable_file

2) Just setting a variable in a script overrides previous settings

3) Why not just explicitly restart the script by calling it instead?

4) You could have the script copy the script, set it executable with chmod, run it, then delete it at the end.

5) Hmm. Dont know. You might need to check whether the output is coming from stdout or stderr (error) for this.

6) Most commands use an integer return for their exit status. The shell variable $? contains the last exit status given.

7) & 8) Dont know, sorry.

9) Use the command fmt

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.