Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

4
votes
2answers
2k views

bash myscript.sh runs in bash, but the first line is #!/usr/bin/sh

I have a script similar following one: #!/usr/bin/sh var="ABC" if [ $var == "ABC" ] then echo True else echo False fi Above code does not work in Solaris Sparc and Solaris X64. It is showing ...
2
votes
7answers
43k views

Adding two numbers using expr

I'm learning Shell scripting for a diploma in IT I'm currently doing. I'm trying to write a small script that adds two numbers as shown as in one of the tutorials we were given. echo "Enter two ...
8
votes
1answer
1k views

Are dash, ash and sh script 100% compatible?

I wanted to know whether scripts written for dash, ash, and sh are 100% compatible? Are there any added features to dash or ash, or syntax change? From what I heard, ash is a direct descendant of ...
1
vote
2answers
362 views

Remove oldest folders + files (50GB) from a folder when share/HD reaches 95% full capacity

I have a big share (~5TB) that is getting full. Now I want to make a script that deletes data from 2 specified folders. But this need to be the oldest files/folders and it need to stop when ~50GB has ...
3
votes
2answers
2k views

When sh is a symlink to bash or dash, bash limits itself to POSIX-compliance, so it should be 100% compatible with sh?

From Difference between bash and sh: ABck to the question: If you have /bin/sh as a link to bash, then bash will not behave the same when called as /bin/sh as it does when called as /bin/bash. ...
6
votes
1answer
293 views

How to find out whether a shell script is written in bash or sh

How can I find out whether a script is written in bash or sh? The first line of the script is not helpful here, since on Linux, bash scripts have this line: #!bin/sh Actually, there are many ...
2
votes
1answer
2k views

Can I run Bash scripts in FreeBSD without modifying them?

Correct me if I'm wrong: "sh" script != "bash" script Linux script are written in Bash Bash script usually #!/bin/sh In GNU/Linux, /bin/sh is Bash In FreeBSD, /bin/sh is not bash, it's the true sh ...
6
votes
3answers
15k views

ssh input from text file

These two question is driving me crazy and I don't have good expertise of ssh. (but I suspect it is to do with redirection only) The questions are, You want to pass multiple lines of input from a ...
6
votes
2answers
2k views

assign shortcut key to run a script

I've written a script to change the brightness of my screen. It's useful when I am working on text mode and shortcut keys of my keyboard to change brightness doesn't work. Is it possible to run this ...
3
votes
3answers
3k views

Shell Script to Execute Shell Script to All Subdirectories

I am making a script to simplify my daily tasks. Everyday, I have to grep for a few things inside a company server. It was okay, however, now, they have segregated each object into sub directories. I ...
1
vote
1answer
396 views

Identify remote windows server from shell script

I need a way to Identify a windows SBS server, I have a script that mounts a file share on a SBS server, I need the script to positivity identify the server before mounting. It doesn't need to be ...
6
votes
2answers
4k views

Why does `exec 2>&1` fail in this bourne shell script?

I am porting an old ksh script to Bourne shell. The old ksh script contains the following code: #!/bin/sh tmpLog=/var/tmp/logfile.$$ exec 1> $tmpLog exec 2>&1 eval $* another_command_1 ...
2
votes
1answer
754 views

Assigning command output to variable gives strange results

My script looks like this: #!/bin/sh PROJECT_DIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P) COMMANDS_CODE=$(php $PROJECT_DIR/bin/console generate:code) echo $COMMANDS_CODE The output has ...
1
vote
2answers
1k views

On the command line, can I redirect output to a file WITHOUT >, >>, or | operators?

I am attempting to run a command on a CentOS virtual machine, and the API that I am using to run this command does not seem to allow the >, >>, or | operators. Also, I must use fully qualified paths ...
0
votes
0answers
2k views

bad magic number

I have a script which I need to execute. The script file is push.sh. Below are the commands that I am using: dragan@dragan-Lenovo-G570:~/liquibase/liquibase$ chmod +x push.sh ...
8
votes
4answers
17k views

printing colored text using echo

I know that for printing a colored text using echo, for example red color, the code is: echo -e "\e[1;31m This is red text \e[0m" and I know that in this example, 31 is code of red color and the ...
17
votes
4answers
21k views

How to introduce timeout for shell scripting?

I want to run a shell script that got a loop in it and it can go for ever which I do not want to happen. So I need to introduce a timeout for the whole script. How can I introduce a timeout for the ...
3
votes
2answers
1k views

How to solve sudo: unable to execute /bin/ls: Argument list too long

When I run this command from php sudo -u db2inst1 ls -t /var/lib/edumate/bdrs/*/*/daily/*NODE* | sort -r I receive sudo: unable to execute /bin/ls: Argument list too long ls lists all daily backup ...
18
votes
1answer
611 views

Is there a standard command that always exits with a failure?

I want to test my script with a command that fails. I could use an existing command with bad arguments. I could also write a simple script that immediately exits with a failure. Both of these are easy ...
2
votes
0answers
443 views

Validate Password for user account from shell script

Application is for a multiple server standardization script Looking for a way in a shell script to check if the password for an OS account (AIX) matches a standardized plain text password. The ...
24
votes
4answers
5k views

How to do nothing forever in an elegant way?

I have a program which produces useful information on stdout but also reads from stdin. I want to redirect its standard output to a file without providing anything on standard input. So far, so good: ...
4
votes
1answer
182 views

Discard stdout of a command for t seconds

I am working on some batch scripts involving the following: Run some non-terminating sub-processes (asynchronously) Wait for t seconds Perform other task X for some time Terminate subprocesses ...
12
votes
1answer
4k views

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
5
votes
3answers
10k views

Automating textual input from a bash script without using EOF

I'm running Ubuntu Linux. Suppose there is a program called myprogram. This program prompts the user for input; specifically, the user must type an integer when prompted and press Enter. I would ...
3
votes
2answers
779 views

is there any command like “wall” that runs a “command” in all terminals

as you know "wall" command sends a message to all terminals: wall message now my question is this: is there any command that can send a "command" to be run in all terminals? for example sending ...
17
votes
2answers
292 views

Shell function definition: why is there a space after the opening brace?

You probably know of that fork bomb: :(){ :|:&};: #WARNING: harmful code I wonder why it is necessary, for it to parse, to include a space after the opening curly brace.
5
votes
1answer
2k views

A couple arithmetic related commands, Dash, POSIX shell, sh, Increment

When pre/post incrementing a variable, in POSIX Shell, what is the difference between these two examples. They seem like they would have the same consequences, but would they be used differently? ...
2
votes
1answer
2k views

How can I set environment variable permanently through shell script?

Let's say I have a script: #!/bin/bash echo $1 if [[ ! $1 ]];then echo "True" fi export ABC="/home/aashish" ABC is not available after the execution of this script. How can I make that variable ...
3
votes
1answer
3k views

Shell script hangs on mail command

I'm finding that a call to the mail command is causing a script to suspend without error. To close the script I have to ctrl-c or issue a kill command on the process id. The pertinent section of ...
1
vote
3answers
338 views

Concise way to output to file if argument is given, to stdout otherwise

I'd like my script to output to stdout unless it gets a filename as an argument. An obvious way is like so: if [ -e "$1" ]; then command_with_output >$1 else command_with_output fi It's ...
4
votes
1answer
6k views

shell backup script, missing `]`

I'm putting together a simple backup script that will tar contents of a folder, then move that file to a backup server. The script makes sure that the tar file exists and is not zero bytes before ...
6
votes
3answers
1k views

How to tweet using terminal?

I would like to tweet a message using terminal. I tried something like: curl -u 'TwitterUsername':'TwitterPassword' -d status=”Your Message Here” https://twitter.com/statuses/update.xml but seems ...
2
votes
1answer
112 views

Restoring an option at the end of a function in zsh

I'm writing a zsh shell function (as opposed to a script) where I would really like the extended_glob option to be enabled. But since the function runs in the caller's context, I don't want to clobber ...
0
votes
2answers
1k views

Re-assigning (specifically, incrementing) a variable in a bash script [duplicate]

Possible Duplicate: How can I do command line integer & float calculations, in bash, or any language available? I would like to simply re-assign a variable (to be specific, increment it ...
2
votes
1answer
448 views

Prompting for input in post-install script in dpkg

I am writing a debian package and need to prompt the user for some textual details(default password, etc), as well as some one-of-many choices("Pick one of the following: foo, bar"). Will shell ...
3
votes
1answer
324 views

Script for emulating Windows [Win]+[Arrow] keyboard shortcut

I am running Fedora 14. I would like to (at least partially) emulate the behaviour of Windows when you hit Win+Arrow Key. That is, I would like it to "snap" to the left/right sides of the screen. I ...
5
votes
2answers
489 views

Automate workflow in shell

Coming from GUI interfaces I've come to finally understand the power of having everything piped in text, but I don't really know how to use all that power yet. I realized the workflow for a ...
10
votes
3answers
4k views

How to sort the string which combined with string + numeric using bash script?

This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected. /home/files/profile1 /home/files/profile10 /home/files/profile11 ...
1
vote
2answers
3k views

Show command executed along with output in log file

Is there a quick and dirty way to not only log the output of a shell file but the commands used inside as well? For example: whoami > who.dmp would output a file that contained something like: ...
6
votes
5answers
3k views

How to create a testcolor.sh like the following screenshot?

I found this image on the internet, but don't know how was it made, Can anyone provide a sample on that? I just would like to have a color table as well.
3
votes
6answers
2k views

Text file look-up by column

I have a file in this format: [#] OWNER_NAME NAME SIZE [6] Robottinosino Software 200 [42] Robottinosino Ideas worth zero 188 [12] ...
11
votes
3answers
8k views

find n most frequent words in a file

I want to find, say, 10 most common word in a text file. Firstly, solution should be optimized for keystrokes (in other words - my time). Secondly, for the performance. Here is what I have so far to ...
5
votes
4answers
6k views

Use a variable reference “inside” another variable

I'm sure it is relatively simple, I just don't know how to do it. #!/usr/bin/ksh set `iostat` myvar=6 I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value ...
3
votes
2answers
2k views

Bash script doesn't work as cronjob

I'm trying to get this simple bash script to work within a cronjob. It's used to generate static nginx webserver statistic pages using GoAccess. I tried everything I know to resolve this issue, it ...
14
votes
2answers
4k views

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code ...
8
votes
1answer
1k views

Suppress output unless non-zero exit code

What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking: quiet_success() { file=/tmp/suppressed if ! ( "$@" > "$file" ...
2
votes
1answer
2k views

Send email from a shell script

I have written a script which runs a simple program in the background, logs the output and emails me when the job is done. It works fine except for the fact I am not recieving the email. I have tried ...
7
votes
2answers
580 views

Shell Script for going through a dir recursively and chmodding based on conditions of file type

Can anyone point me to either code or a tutorial for writing a shell script that can recursively go through an entire directory structure (starting at the current working directory, or given an ...
2
votes
4answers
2k views

Run if statement on remote machine

I'm trying to run a set of commands on a remote machine, that includes an if statement. I'm using this to shutdown a list of kvm instances. The first line should be fine, but could be prettier, rest ...
0
votes
1answer
271 views

Checking existence of file with underscore in file name in zsh

I am writing a very simple script which will link shared configuration files. For some reason it does not behave as i expected for file with underscore in name. Does _ (underscore) means something ...