Changing the current working directory of invoking bash programmatically [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file which contains key/value -pairs in the format
TAG PATH
. Now, I want to write a program that reads from the command line a TAG, and changes the current working directory of invoking bash to corresponding PATH.
How would I go about this?

You might consider something like (perhaps in your bash function)
while read tagname dirname ; do
pushd $dirname ;
dosomethingwith $tagname
popd
done < yourinputfile.txt
See this question (about read in bash) and read the advanced bash scripting guide
GNU awk might be a better tool.
Notice that you can only change the current directory of the current process using the chdir(2) syscall (invoked by cd or pushd or popd bash builtins). There is no way to change the directory of some other process (e.g. the parent or invoking one). The pushd and popd builtins also updates bash directory stack.
There is no way to change the current directory of the invoking shell (the parent process running in a terminal). But you may define your own bash function there. Read Advanced Linux Programming to understand more about Unix processes

Related

Why `which pushd` doesn't return anything? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
My understanding is that all commands in linux must exist on the $PATH, even for the most basic commands
> which cd
/bin/cd
> which ls
/bin/ls
But when I tried which pushd, to my surprise, it returned:
/usr/bin/which: no pushd in (/bin:/usr/share/maven/bin:/usr/share/java/jdk1.8.0_131/bin:/usr/local/bin:/usr/bin:/usr/local/sbin)
pushd is "installed" and working. This challenges my whole understanding of linux commands.
Can someone explain why this is happening?
Can someone explain why this is happening?
pushd, like many other commands, is a builtin. which is itself an executable and which searches for executables - there is no such executable as pushd.
To affect the current working directory of the shell itself, it has to be a builtin, just like cd.
You can check what it is with type:
$ type pushd
pushd is a shell builtin
what are other examples of such shell builtins?
They are listed in documentation: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Builtin-Commands .

How can I create .sh extension file in Linux Ubuntu? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a script to run one of my .jar files as daemons, but I am not understanding how to create a .sh extension file in Ubuntu. I have already used vi to create a file with the code that I want, but I cannot figure out how to define the file to specifically be a .sh file. For example, I want to convert my file "foo" or "foo.txt" into "foo.sh".
Is there a simple command I am not seeing that can convert files to a .sh extension or is it a more complicated process?
Use this as a template:
#!/bin/bash
# content of your script
The first line is called a shebang and tells the OS what program that should be used executing the content of the file, in this case, bash.
To make the file executable:
chmod 755 foo.sh
To execute your script do:
./foo.sh

Linux shell script erroroing out due to /usr/bin/sh [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to run a shell script on redhat linux to install an app. I am getting an error saying /usr/bin.sh: bad interpreter: no such file or directory.
In the shell script the script begins with:
#!/usr/bin/shBUILD_ID=$1.....
I am just trying to understand what the path at the begining of the line is for? Is that a directory it looks for to deploy the app?
Thanks
The first line should be #!/usr/bin/sh or #!/bin/sh if its a shell script.
If the first line are #!/usr/bin/sh then try to see if /usr/bin/sh exist and you with ls -l /usr/bin/sh
If you cant find sh then your system are in a bad stat.
The #! is a magic number that tells the kernel that the file is an excutable script.
The string immediately following the #! is the path to an interpreter that is called to read and execute the contents of the file. In the line
#!/ust/bin/shBUILD_ID=$1.....
the interpreter is /ust/bin/shBUILD_ID=$1..... The interpreter is read directly, with no shell variable substition, so it will look for a file exactly as you specified (including the equal sign, dollar, dots etc.). If the interpreter you specified is not found, a default shell issues an error message, and yours looks totally wrong. Try #!/bin/sh.
If the interpreter string is followed by a space and then some arguments, those arguments are passed to the interpeter when it is invoked.
See for example http://bash.cyberciti.biz/guide/Shebang

rename multiple files shell in Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a number of files such as file_022.bmp, file_023.bmp...file_0680.bmp. I need to rename these to something a little bit more convenient such as file_1.bmp, file_2.bmp...file_658.bmp.
Is there a bash script that I could write to do this for me? Thanks for the help and advice.
Luke H
if you're on a debian based linux system then you can use the rename script which accepts regular expressions to rename files. Some more info because I find it hard to find the man page.
e.g.
harald#Midians_Gate:~$ ls p*.php
parse.php pd.php pgrep.php preg_based.php proc.php
suppose I want to change the extension to .perl and prepend the name with file_
then I use command:
rename -n 's/([a-z]*)\.php/file_$1.perl/' p*.php
would give
parse.php renamed as file_parse.perl
pd.php renamed as file_pd.perl
pgrep.php renamed as file_pgrep.perl
preg_based.php renamed as preg_file_based.perl
proc.php renamed as file_proc.perl
I select and capture the base filename ([a-z]*) and then use it in the substitution $1 and append .perl and prepend $1 with the regular string file_
the -n option makes it test run without changing anything
As you can see from this example your selecting regexp needs to be correctly thought out or you get cases like the above preg_based.php where you wanted file_preg_based.perl :)
to compensate for that I would've needed to use ([a-z_]*) here
It's one of the many reasons why I keep hanging on to debian, I'd love to find the equivalent for other non-debian systems though :-/
if you have files a.bmp,b.bmp,c.bmp
and you want to end up with file_1.bmp, file_2.bmp, file_3.bmp
using bash:
mkdir result
index=1
for i in *.bmp
do
mv "$i" "result/file_"$((index++)).bmp
done
notes:
using a subdirectory is advised to avoid accidentally overwriting a file that looks like file_xx.bmp
if you have too many files to fit in the command line after expansion you could use something like:
mkdir result
index=1
find . -name "*.bmp" | while read i
do
echo mv "$i" "result/file_"$((index++)).bmp
done
after inspecting the output remove the 'echo'

Shell tool to move in a complex directory structure [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
My development machine is a linux host.
I have a complicated directory structure (like most of you, I assume), and I would like to move easily from one directory to the other, from within the shell. Specifically, welcomed features would be:
autocompletion (something like ido-mode in emacs)
regular expression directory / file matching
suggestion of recently visited directories (stack).
Possibilty to push/pop to the stack, get a listing of recently visited directories, ...
good integration of those features
console based
Do you know any tool which can satisfy those requirements?
In bash you can set CDPATH to a colon-separated directories that bash will search for when the argument to the cd does not exist.
$ man bash|grep -A3 '^\s\+CDPATH '
CDPATH The search path for the cd command. This is a colon-
separated list of directories in which the shell looks
for destination directories specified by the cd com‐
mand. A sample value is ".:~:/usr".
Once set, autocomplete will just work the way you'd expect it:
$ export CDPATH=dir1:dir2
$ cd somedir<tab>
Besides the current directory, bash will look into the directories in $CDPATH for the possible values.
Umm, any interactive shell(say, bash) already has nearly all of these features:
Press Tab once to auto-complete, and twice to show a list of possible completions.
find | grep reg.exp can be used for file matching, or find -exec grep reg.exp -H '{}' ';' to match contents
You can switch to the previous directory with cd -
pushd and popd can be used to push and pop directories

Resources